Python File Operation
Files
Files are objects on a computer that stores data. They are used to permanently store data in non-volatile memory (hard disk).
As we know, RAM (Random Access Memory) is volatile, which loses its data when the computer is shut down. This is why we use files to store data permanently.
Before reading from or writing to a file, we need to open it first. After finishing our operation with a file, we need to close it so that the resources attached to the file are released.
In Python, to interact with a file, we need to follow the following steps:
- Open a file
- Read from or write to a file
- Close the file
Opening Files in Python
The built-in open()
function is used to open a file. It returns a file used to read or modify the file accordingly.
In the following example, we will open a file by specifying the file in the current directory and the full path of the file.
>>> f = open("demo.txt") # open file in current directory
>>> f = open("/home/expectocode/example.txt") # specifying full path
The open()
function takes two parameters, filename and mode.
There are four different modes for opening a file.
Mode | Action | Description | Remark |
---|---|---|---|
"r" | Read | Opens a file for reading. (default) | Returns an error if the file does not exist |
"a" | Append | Opens a file for appending | Creates the file if it does not exist |
"w" | Write | Opens a file for writing | Creates the file if it does not exist |
"x" | Create | Create the specified file | Returns an error if the file exists |
"+" | Update | Opens a file for updating (reading and writing) | The "+" mode is used in addition with "r" or "w" |
Also, you can specify if the file should be handled as binary or text mode.
Mode | Opening Type | Description | Example |
---|---|---|---|
"t" | Text | Opens a file in text mode. (default) | a text file |
"b" | Binary | Opens a file in binary mode | an image |
Syntax
You can specify only the name of the file to open it.
>>> f = open("demo.txt")
The above code is the same as:
>>> f = open("demo.txt", "rt")
The above code uses the "r" (read) and "t" (text), which are the default mode values, so we do not need to specify them.
Note:
- When opening a file with "r" (read) mode, make sure the file exists, or else you will get an error.
- When opening a file with "x" (create) mode, make sure the file does not exist, or else you will get an error.
File encodig
In Python, the character d
does not imply the number 100 until it is encoded using ASCII (or other equivalent encodings).
The default encoding in python is platform-dependent. In Linux is utf-8
, and in Windows is cp1252
.
So when working with files in text mode, it is recommended to specify the encoding type.
f = open("demo.txt", mode='r', encoding='utf-8')
Note: You must not rely on the default encoding, or else your code will behave differently in different platforms.
Closing Files in Python
After finishing working with a file, we need to close the file correctly.
After closing a file, the resources that were attached to the file become free.
To close a file, python provides the close()
method.
Python has a garbage collector to clean up unreferenced objects, how you must not rely on it to close the file.
f = open("demo.txt", encoding = "utf-8")
# close a file
f.close()
When interacting with a file, an exception can occur. When it does, the code will exists without closing the file.
To overcome exceptions raised when interacting with files, we use the try...finally
block.
try:
f = open("demo.txt", encoding = "utf-8")
finally:
f.close()
In the above code, using the try...finally
block, we ensure that the file is closed correctly even if an exception is raised.
The best approach to close a file is by using the with
statement. This guarantees that the file is closed when the block inside the with
statement is exited.
Using the with
statement, we do not need to explicitly call the close()
method.
with open("demo.txt", encoding = "utf-8") as f:
# perform file operations
Writing to Files in Python
Before writing to a file, we need to open the file with one of the following modes: w
(write), a
append, or exclusive x
(creation) mode.
Opening a file with the w
mode will overwrite the file if it already exists. So be cantions using this mode because all the previous data will be rased.
To write a string or sequence of bytes (for binary files) into a file, we use the write()
method. The write()
method returns the number of characters written to the file.
with open("demo.txt", "w", encoding = "utf-8") as f:
f.write("the first line\n")
f.write("the second line\n")
f.write("the last line\n")
In the above code, we created a file named demo.txt
in the current directory. We used the w
mode to open the file, so if the file does not exist, it will be created; otherwise, if it does exist, it will be overwritten.
We can also notice that we used the \n
to include a new line.
Reading Files in Python
Before reading a file in Python, we must open the file with r
(reading) mode.
Python provides different methods to read a file. We can use the read(size)
method to read in the size
number of data. If the size
parameter is not specified, the read()
method will read and return up to the end of the file.
In the following example, we will read the demo.txt
file we created in the above section.
with open("demo.txt", "r", encoding = "utf-8") as f:
print(f.read(3)) # read the first 3 data
print(f.read(6)) # read the next 6 data
print(f.read()) # read in the rest to the end of file
print(f.read()) # further reading returns empty string
Output
the
first
line
the second line
the last line
As we can see above, the read()
method returns a newline as it encounters a \n
. After reaching the end of the file, we get an empty string on further reading.
To change the current file cursor (position), we can use the seek()
method. The tell()
method returns our current position (in a number of bytes).
with open("demo.txt", "r", encoding = "utf-8") as f:
print(f.tell()) # get the current file position
print(f.read())
print(f.tell())
print(f.seek(0)) # bring the cursor to the initial position
Output
0
the first line
the second line
the last line
45
0
We can also read a file using a for loop, which is a fast and efficient way to read a file.
with open("demo.txt", "r", encoding = "utf-8") as f:
for line in f:
print(line, end = "")
Output
the first line
the second line
the last line
In the above code, the lines in the file include a newline character \n
. This is why we used the end
parameter of the print()
function to avoid two newlines when printing.
We can also use the readline()
method to read individual lines of a file. The readline()
method reads a file to the newline, including the newline character.
with open("demo.txt", "r", encoding = "utf-8") as f:
print(f.readline(), end = "")
print(f.readline(), end = "")
print(f.readline(), end = "")
Output
the first line
the second line
the last line
The readlines()
method returns a list of the remaining lines of the entire file.
with open("demo.txt", "r", encoding = "utf-8") as f:
print(f.readlines())
Output
['the first line\n', 'the second line\n', 'the last line\n']
Note: All the reading methods return empty values when the end of file
EOF
is reached.
Python File Methods
Python provides various methods to interact with a file.
Here is a complete list of methods in text mode with a short description.
Method | Description |
---|---|
open() | Opens a file. It returns a file used to read or modify the file. |
close() | Closes an opened file. It has no effect if the file is already closed. |
detach() | Separates the underlying binary buffer from the TextIOBase and returns it. |
fileno() | Returns an integer number (file descriptor) of the file. |
flush() | flushes the write buffer of the file stream. |
isatty() | Returns True if the file stream is interactive. |
read(n ) |
Reads at most n characters from the file. Reads to the end of the file if it is negative or None . |
readable() | Returns True if the file stream can be read from. |
readline(n = -1) |
Reads and returns one line from the file. Reads in at most n bytes if specified. |
readlines(n = -1) |
Reads and returns a list of lines from the file. Reads in at most n bytes/characters if specified. |
seek(offset , from = SEEK_SET ) |
Changes the file position to offset bytes, in reference to from (start, current, end). |
seekable() | Returns True if the file stream supports random access. |
tell() | Returns the current file location. |
truncate(size = None ) |
Resizes the file stream to size bytes. If size is not specified, resizes to the current location. |
writable() | Returns True if the file stream can be written to. |
write(s ) |
Write the string s to the file and returns the number of characters written. |
writelines(lines ) |
Writes a list of lines to the file. |