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()
with open("hello.txt", "w") as f:
f.write("Hello World")
#using With Statement files opened will be closed automatically
# 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
with open(filename,"w") as f:
f.write('Hello World')
file = open("directory/sample.txt", "w")
file.write(“Hello World”)
file.close()
# Opening a file
file1 = open('SofthuntFile1.txt', 'w')
multiple_string = ["This is Mango
", "This is Apple
", "This is Banana
"]
single_string = "Hi
"
# Writing a string to file
file1.write(single_string)
# Writing multiple strings at a time
file1.writelines(multiple_string)
# Closing file
file1.close()
# Checking if the data is written to file or not
file1 = open('SofthuntFile1.txt', 'r')
print(file1.read())
file1.close()