Files
Python allows us to work with files natively, so we can store information for later, or read and analyze data .
In this lesson, we will cover the basics of opening, reading, and writing to files in Python.
File Basics
Before we can start, it is good to know a few concepts of files on a computer.
- All files on a computer will have a path.
- At the end of the path will be the file name.
- Files can have an extension that declares the file type.
Depending on your operating system, file paths can look like:
/desktop/folder/file.txt
for Mac and Linux\desktop\folder\file.txt
for Windows
In both cases, the file we are looking for is located on the desktop in a folder named folder
. Additionally, both files have the same name file
and both are plain text documents, as denoted by the .txt
at the end.
Python supports both types of file paths on their respective platforms. If a file is in the same folder as your Python program, then there is no need to include a path to that file.
File Modes
Python gives us a few different options when it comes to opening files. Listed are some of the most common ones:
Mode | Description |
---|---|
r | reads the file |
w | writes to the file |
a | appends to the end of the file |
r+ | reads and writes to the file |
By default, calling the open
function will set the file to read mode.
Reading Files
To read a file in Python, we have to first call a function called open
and provide it a file name or file path, then set it equal to a variable. For example:
file = open("hello.txt")
The above code will open a file called hello.txt
, located in the current folder, and will store a file object in the variable file
.
We will discuss more on what objects and classes are in a later lesson
To access the content of the file, simply call read method.
file = open("hello.txt")
print(file.read())
Note: The read function works like a cursor, thus once you call the read function, calling it again will output nothing. You would have to move the cursor back to the first character by calling the seek
function.
file = open("hello.txt")
print(file.read())
file.seek(0)
print(file.read()) # Now works again
Writing to Files
In Python, writing to files is as easy as reading a file. We start by opening a file, but this time also providing the writing argument:
file = open("bye.txt", "w") # w stands for write
Now to write to a file, simply call the write method:
file = open("bye.txt", "w")
file.write("Hello!")
file.write("Good Bye!")
By Default, Python does not put a new line after writing to a file, so the contents of bye.txt
would look like Hello!Good Bye!
.
To put a new line at the end of a write, we must add the new line symbol \n
at the end of the line. This would look like:
file = open("bye.txt", "w")
file.write("Hello!\n")
file.write("Good Bye!\n")
Closing Files
At some point in our program, we will have to close connections to files, when in Python.
To close the files in the previous two examples, simply call the close
methods.
file = open('example.txt')
# Do stuff here
file.close()