write file with python

Do you want to know the details regarding “write file with python”. If yes, you’re in the correct tutorial.

write file with python

file = open(“testfile.txt”,”w”) 
file.write(“Hello World”) 
file.write(“This is our new text file”) 
file.write(“and this is another line.”) 
file.write(“Why? Because we can.”) 
# using 'with' block

with open("xyz.txt", "w") as file: # xyz.txt is filename, w means write format
  file.write("xyz") # write text xyz in the file
# maunal opening and closing

f= open("xyz.txt", "w")

# Hope you had a nice little IO lesson
with open("testfile.txt", "w") as f:
  # "w" - write into file
  # "r" - read into file
  # "+" - read and write into file
  f.write("Hello World")
Simple way to get a directory of a file and open it:
from tkinter.filedialog import askopenfilename

filename = askopenfilename()

Final Words

I hope this post helps you to know about “write file with python”. If you have any doubts regarding this tutorial please let us know via the comment section. Share this tutorial with your friends & family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment