Reading and Writing Files in Python

From Sustainability Methods
Revision as of 15:36, 4 July 2023 by Milan (talk | contribs) (This article provides an introduction to reading and writing files in Python)

THIS ARTICLE IS STILL IN EDITING MODE

Reading Files

There are different types of file systems in many operating systems. However, most of the time, you will get off the hook when knowing the two below:

File System Extension Code Use
Text .txt text file, for stories, documents
Comma Separated Value .csv exported excel file, structured data, data from sensors, and numbers

First, you need a file to read, so let’s create a file.

Open the folder in which you wish to store the file. When right-clicking on an empty space, you are able to create a new file. Name it test_text.txt and open it. After that write something inside the file, save, and close it. Alternatively, you can also open the program that creates text files (for Windows 10 it is "Notepad") and create a new file by left-clicking on the file tab in the left corner and choosing "New" or press CTRL+N (both for Windows 10). After the file has been created, we can try to read from that particular file.

To read files from your computer to Python, there is a built-in function called “open”.

file = open("test_text.txt")

Don't forget that you have to specify the working directory when first opening the file. Before the "test_text.txt" (in the same quotation marks) you need to provide the file path so that Python knows where to find your text file Now, you can check if the file has been successfully read: First, you can read the file name:

file.name

And then, of course, you can also check out the content of the file:

print(file.read())

Once you have finished your work with the file, we need to “close()” the connection manually because the “open()” method uses some computer resources to clean up.

file.close()

WITH Statement

The WITH statement is a python specific statement that manages and simplifies the opening and closing of files. Let's compare 2 codes:

# code without WITH statement
file = open ("test_text.txt", "r")
print(file.read())
file.close()


#code with WITH statement
with open ("test_text.txt", "r") as file:
		print(file.read())

With the WITH statement, the close() method is automatically included. It also makes the code easier to read because of indentation.

Writing to Files

Besides just reading from the file, we can also write to a file.

wr_file_ex = 'new_file.txt' # write file example
with open(wr_file_ex , "w") as writefile:
    writefile.write("this is the first line \\n")
		writefile.write("second line\\n")

The code above will write 2 lines. Note that inside the “open()” method, we have to specify “w” (write) as an argument. In the very beginning, we have not specified any mode, which is why Python automatically used the reading mode. In the table at the end of this tutorial, you can find different modes you can use in Pyhthon.

We can also try to check what we have created:

lines = ["First line!\n", "Second line!\n", "Third line!"]

with open(wr_file_ex, 'w') as writefile: 
		for line in lines: 
				writefile.write(line)

# and you can read the file using the read method
with open (write_file_ex, 'r') as read_file:
		print(read_file.read())

Warning: setting a file to mode “w” will overwrite all data inside.

old_file = "test_text.txt"
with open (old_file,"r") as read_file: 
		print(read_file.read())
# Write line to file
with open(old_file, "w") as write_file:
    write_file.write("overwritten\n")

# read again afterwards
with open (old_file,"r") as read_file: 
		print(read_file.read())

Appending

As we know from the exercise above, writing overwrites all the old lines or data. Many times, we don't want the old data to be overwritten, and we just want to add a new line or new data. That’s where appending comes in.

# appending new lines to the old file
old_file = "test_text.txt"
with open (old_file, 'a') as append_file: 
		append_file.write("another line\\n")
		append_file.write("another another line\\n")

# reading the file after new lines have been appended
with open (old_file, 'r') as read_appended_file: 
		print(read_appended_file.read())

Different modes when opening a file

Here is a table with the most important different modes when opening a file (we have covered almost all of them). Note that the last three are more advanced modes. They allow you to combine two different modes and you don't have to use the WITH statement whenever you want to combine two modes.

Statement Mode name Use
w read mode Overwriting files
r write mode Reading files
a append mode Adding new content to file
x exclusive creation mode Creating a new file and opening it for writing
r+ reading and writing mode Reading and writing, does not truncate file
w+ writing and reading mode Writing and reading, truncates file
a+ appending and reading mode Creates new file if the file does not exist

Quiz

  1. Try out a+ mode by running this:
# quiz a+ mode
with open('Quiz.txt', 'a+') as write_file:
    write_file.write("A whole new line\n")
    print(write_file.read())

Can you see the printed line? If not, then you’re correct! There is no error, but the “print()” also does not output anything.

Try it again with the r+ mode:

# quiz r+ mode
with open('Quiz.txt', 'r+') as write_file:
    write_file.write("A whole new line\n")
    print(write_file.read())

Can you now see the printed line? How could you find out if you have written anything in the a+ mode?

The author of this entry is XX. Edited by Milan Maushart