Horje
write a file python Code Example
python write to file
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.”) 
 
file.close() 
python write to file
# 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")
f.write("hello")
f.close()

# Hope you had a nice little IO lesson
write a file python
# read, write, close a file
# catch error if raise
try:
    file = open("tryCatchFile.txt","w") 
    file.write("Hello World")

    file = open("tryCatchFile.txt", "r")
    print(file.read())
except Exception as e:
    print(e)
finally:
    file.close()
how to file in python
Simple way to get a directory of a file and open it:
  
from tkinter.filedialog import askopenfilename

filename = askopenfilename()
open and write in a file in python
my_file = open("C:\\Users\\Python\\file.txt", "w")
#Give the path accurately and use \\
my_file.write("This is the test text")
my_file.close()

# It is always recommended to close the file after modifying
# to see the output, open the file - file.txt




Python

Related
read a file python Code Example read a file python Code Example
collections counter Code Example collections counter Code Example
np.rand.randint Code Example np.rand.randint Code Example
discordpy get role by id Code Example discordpy get role by id Code Example
delete a key value pair from a dictionary in python Code Example delete a key value pair from a dictionary in python Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7