1.1 Opening a File
Working with files in Python is a breeze because it's pretty simple and intuitive. We're not talking about creating or moving files around on the disk, but about reading and writing file content.
The usual workflow for dealing with file content is as follows:
Opening a file
Working with file data
Closing the file
Example of reading a file's entire content:
file = open('example.txt', 'r')
content = file.read()
print(content)
file.close()
The open function is used to open a file. It takes two primary arguments: the file name and mode of access. The function returns a file object, which you can then use for performing read and write operations.
Example:
file = open('example.txt', 'r')
If only the file name is specified, it will be searched for in the program's current working directory. If the full file name is given, it will access it via the specified path.
1.2 Access Modes
The second parameter in the open() function indicates the file access mode. Access modes determine what operations can be performed on the file (reading, writing, etc.).
Here are the main access modes:
Read— reading.Write— writing.Append— appending.
There are several variations of these modes:
- 'r': Open file for reading (default mode). The file must exist.
- 'w': Open file for writing. The file content will be erased. If the file doesn't exist, it'll be created.
- 'a': Open file for appending. Data will be added to the end of the file. If the file doesn't exist, it'll be created.
- 'r+': Open file for reading and writing. The file must exist.
- 'w+': Open file for reading and writing. The file content will be erased. If the file doesn't exist, it'll be created.
- 'a+': Open file for reading and appending. Data will be added to the end of the file. If the file doesn't exist, it'll be created.
Example of writing to a file:
If you want to write data to a file, just specify w as the access mode when opening the file. Note that all the old content of the file will be erased!
file = open('example.txt', 'w')
file.write("Hello, World!")
file.close()
Example of appending to a file:
If you don't want to delete existing file content but instead want to add something to its end, just specify a as the file access mode.
file = open('example.txt', 'a')
file.write("\nAppended text.")
file.close()
Important! If you write to a file that doesn't yet exist, it will be created automatically. But if you try to read from a non-existent file, you'll get an exception.
1.3 Closing a File
Every time you open a file, the operating system checks your access rights to the file and marks it as being used by your program. A special resource, called a handler, is allocated for file operations.
The number of handlers your program can have is limited. This means you can only keep a few hundred files open simultaneously. It might seem like a lot, but if your program runs on a server for months and reads and writes files daily, this limit can be reached quickly.
Therefore, after you’re done working with a file, it should be closed by calling the close() method. This way, you inform the operating system that you no longer need the file and its handler.
Closing a file
After finishing working with a file, you need to close it to free system resources. Use the close() method for this.
file = open('example.txt', 'r')
# Performing operations with the file
file.close()
Every file you've opened for reading or writing should be closed. Don't forget to call the close() method.
GO TO FULL VERSION