1file = open(“testfile.txt”,”w”)
2
3file.write(“Hello World”)
4file.write(“This is our new text file”)
5file.write(“and this is another line.”)
6file.write(“Why? Because we can.”)
7
8file.close()
1with open("hello.txt", "w") as f:
2 f.write("Hello World")
3#using With Statement files opened will be closed automatically
1with open("test.txt",'w',encoding = 'utf-8') as f:
2 f.write("my first file\n")
3 f.write("This file\n\n")
4 f.write("contains three lines\n")
1# using 'with' block
2
3with open("xyz.txt", "w") as file: # xyz.txt is filename, w means write format
4 file.write("xyz") # write text xyz in the file
5
6# maunal opening and closing
7
8f= open("xyz.txt", "w")
9f.write("hello")
10f.close()
11
12# Hope you had a nice little IO lesson
1path = "guide/README.txt" # The path of your file should go here
2with open(path, "w") as fil: # Opens the file using 'w' method. See below for list of methods.
3 fil.write("This is the README. It is reccomended that you read it.") # Writes to the file used .write() method
4 fil.close() # Closes file
5'''
6List of methods:
7w* - replace everything with needed text
8r^ - read the file
9a* - adds to file
10x - creates file
11
12* Creates file if the file at that path does not exist
13^ Throws error if file does not exist
14'''
15
1with open("testfile.txt", "w") as f:
2 # "w" - write into file
3 # "r" - read into file
4 # "+" - read and write into file
5 f.write("Hello World")
1# open a file you can use the function open
2file = open("myFile.txt", "w")
3#here we have "open", and that's the main function
4# that opens the file, next we have 2 arguments
5# FILENAME and OPENING MODE
6# in the filename argument you just have to write the file's location
7# or if the script is in that location just write the filename
8# in the opening mode you have to write in which mode you want
9# to open your file, i'll list some here:
10# "w" for writing to a file
11# "r" for reading to a file
12# "r+" for both reading and writing
13# "a" to append to a file
14#to write to the file use "file.write"
15file.write("This has been written by a program")
16#and finally to close the file when you're done with it
17file.close()
18# hope this helped and remember that in "w" mode it
19#deletes the content of the file and replaces
20# it with a new one, if you want to add something
21# to a file use "a" mode