Python - How to Read and Write a Text File
Reading and writing text files is easy in Python. Python gives you a lot of control over how you handle file IO. We’re going to cover the most basic, common use cases first, then we’re going to cover more use cases and get into the details.
Reading a File
You can read a file like this:
f = open('my-file1.txt', 'r') data = f.read() f.close() print(data)
Reading a file is probably one of the most basic file IO operations that you could possibly want to do. In this example we start out by opening the file. First specify the file name. Then specify how you want to access the file ( ‘r’ for read ). This creates a file handle that we save in the variable ‘f’. We then use the file handle to call read() which reads the entire file into a variable ( stored in memory ). Finally, we close the file handle which is important.
Writing to a File
You can write to a file like this. It is similar to reading from a file except that you specify ‘w’ instead of ‘r’ and you aren’t reading the data into a variable.
f = open('my-file1.txt', 'w') f.write('This is a test') f.close()
You can use the following syntax if you don’t want to have to remember to close a file. Any operations done with the file handle are done in the indented block after the ‘with’ statement. After that block the file handle is closed. You can save the data to a variable and use it outside the block.
with open('my-file1.txt', 'r') as f: data = f.read() print(data)
Read Lines as a List
You can read each line in a file into a list. Once you’ve done this you can then just loop over the list. Keep in mind that this would be stored in memory.
with open('my-file1.txt', 'r') as f: a = f.readlines() for i in a: print(i)
Reading Large Files
If you have a huge file you probably don’t want to just read the entire thing into memory before using it. You are going to want to read it line at a time and process each line as it comes in.
with open('my-file1.txt', 'r') as f: data = f.readline() print(data) #process_data(data) while data: data = f.readline() print(data) #process_data(data)
Write a List to a File
If your data is stored as a list of strings, you can write them all to a file like this:
with open('my-file1.txt', 'w') as f: f.writelines(a)
Writing a Large Amount of Data
If you have a lot of data that needs to be written to a file you probably don’t want to just store it all in memory and then write that to the file. You are probably going to want to write that data to the file as it is generated.
with open('my-file1.txt', 'w') as f: while( more_data ): x = generate_data() f.write(x)
File Access Modes
|r||Open for reading ( default ), error if doesn’t exist|
|r+||Open for reading and writing, error if doesn’t exist|
|w||Open for writing ( overwrites )|
|w+||Open for reading and writing ( overwrites or creates )|
|a||Open for append, writes to end of file, creates|
|a+||Open for append and read, writes to end of file, creates|
|x||Create, error if it exists|
File IO Functions
|open(‘my-file1.txt’, ‘w’)||Open a file and create a file handle|
|write(‘test’)||Write string to file|
|writelines(a)||Write array of strings to file|
|read(5)||Read 5 bytes|
|readline()||Line at a time|
|readline(5)||Line at a time, max 5 bytes per line|
|readlines()||Entire file, as list|
|seek(34)||Move to this byte in the file|
|seek(0)||Back to the begining|
Your paths might look like any of these:
"my-file1.txt" "D:\\myfiles\my-file1.txt" "/home/user1/my-file1.txt"
You can use a raw string to avoid special characters:
f = open(r"my-file1.txt", "w")