In this article, we will discuss how to perform different file operations in Python. We will keep this to-the-point.
Reading a file.
f = open("sample.txt", "r")
content = f.read()
print(content)
f.close()
Writing to a file.
f = open("test.txt", "w")
f.write("pythoncircle.com")
f.close()
Closing a file descriptor is necessary otherwise it may lead to memory leaks. Sometimes reading or writing operations may throw an exception. In such a scenario, the file will not be closed properly. This may lead to unexpected behavior of the program and can lead to a resource leak.
To make sure the file is closed every time it is opened, we should follow one of the below approaches.
1. Use finally.
try:
f = open("test.txt", "w+")
f.write("123123123")
except Exception as e:
print(e)
finally:
f.close()
2. Use context manager with open
with open("sample.txt", "r") as f:
content = f.read()
print(content)
with open("test.txt", "w") as f:
f.write("hello")
Read more about content managers here.
So when we open a file using with, we need not worry about the closing of the file. All the cleanup is automatically performed by Python.
read
method.
with open("large_file.txt", "r") as f:
print(f.read())
read()
method read the whole content of the file in one go. This method also accepts a parameter, size
. This is the number of characters or bytes to read.
readline
method.
with open("large_file.txt", "r") as f:
print(f.readline())
readline()
method read only one line at a time. This method also accepts a parameter, size
. This is the number of characters or bytes to read.
To get all the lines, use a loop until we are getting non-empty lines.
Example of reading a file one line at a time.
with open("large_file.txt", "r") as f:
line = f.readline()
while line != "":
print(line)
line = f.readline()
readlines
method.
with open("large_file.txt", "r") as f:
print(f.readlines())
readlines()
method also fetches the whole content of the file in one go. The return type of this method is a list of lines.
Output example.
['largefile line 1\n', 'largefile line 2\n', 'largefile line 3']
hint
. readlines
method stops reading lines once the number of characters exceeds more than hint
.with open("large_file.txt", "r") as f:
print(f.readlines(25))
readline
method is memory efficient as it reads one line at a time.write
method.
with open("write_example.txt", "w") as f:
f.write("hello")
Write the content to the file.
writelines
method.
names = list()
names.append("Anurag")
names.append("Ashutosh")
names.append("rana")
with open("names_example.txt", "w") as f:
f.writelines(names)
Write the list of lines/text to the file.
We have a few predefined modes in which we can operate on file.
r -> open for reading (default)
r+ -> open for both reading and writing (file pointer is at the beginning of the file)
w -> open for writing (truncate the file if it exists)
w+ -> open for both reading and writing (truncate the file if it exists)
a -> open for writing (append to the end of the file if exists & file pointer is at the end of the file)
rb -> Opens a file for reading in Binary format.
wb -> Opens a file for writing in Binary format.
wb+ -> Opens a file for writing and reading in Binary format.
tell()
method is used to find the current location of the cursor within the file.
seek(offset, whence)
method is used to move the pointer with the given offset, respective to the whence.
whence can have one of the below values.
0 -> from the starting of file.
1 -> current location of the cursor.
2 -> from the end of the file. This value can be used in binary mode only.
with open("large_file.txt", "r") as f:
print(f.tell())
print(f.seek(2))
print(f.tell())
print(f.read())
the output will be.
0
2
2
rgefile line 1
largefile line 2
largefile line 3
seek
operation.tell
operation.read()
method. Notice the missing 2 characters which have been skipped due to seek()
operation.with open("large_file.txt", "rb") as f:
print(f.tell())
print(f.seek(-10, 2))
print(f.tell())
print(f.read())
Also, note that file mode should be binary, 'rb' in the above example. If you try to use the seek operation on a file opened in text mode i.e. 'r' mode, you will get the error "can't do nonzero end-relative seeks".
with open("large_file.txt", "r") as f:
print(f.tell())
print(f.seek(-10, 2))
print(f.tell())
print(f.read())
Output:
Traceback (most recent call last):
File "files.py", line 108, in <module>
print(f.seek(-10, 2))
io.UnsupportedOperation: can't do nonzero end-relative seeks
stats
function of os
module to check the stats of a file.import os
print(os.stat("files.py"))
os.stat_result(st_mode=33204, st_ino=11278227, st_dev=66308, st_nlink=1, st_uid=1000, st_gid=1000, st_size=3438, st_atime=1598281823, st_mtime=1598281815, st_ctime=1598281815)
stat_result
object contains the mode, inode number, number of links, user id, group id, size in bytes, access time, modified time, and creation time.system()
function of os
module or copy
function of shutil
module.import os
os.system("cp file1 file2")
import shutil
shutil.copy("file1", "file1_copy")
copy
function will not copy the metadata of file like access time, modified time.import shutil shutil.copy2("file1", "file1_copy2")
copy2
function will copy the metadata like access time or modified time of the file along with the file. This can be verified using the os.stats("file")
function call.import shutil shutil.copyfile("t.txt", "t_copyfile")
copyfile
function will not copy the permissions of the file. The newly copied file will have default system permissions.system
method of os
module.os.system("mv source destination")
rename
method of os
module.os.rename("sourcepath", "destination path")
move
method of shutil
method.shutil.move("source", "destination")
remove
method of os module.os.remove("path")
system
method of os module.os.system("rm pathname")
os
module can be used to perform all file operations. Not only file operations, but we also can perform many other operation system related tasks using os
module.os.walk(path)
function.Host your Django Application for free on PythonAnyWhere. If you want full control of your application and server, you should consider DigitalOcean. Create an account with this link and get $100 credits.
Feature image source: https://unsplash.com/photos/05HLFQu8bFw