1000 Python Questions
Get 1 Python question daily. Join this telegram channel https://t.me/python1000questions
Advertise with us
file read write   0   9166
Read, write, tell, seek, check stats, move, copy and delete a file in Python

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.



Closing a file.

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.



3 ways to read a file.

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']
This method also accepts a parameter, hint. readlines method stops reading lines once the number of characters exceeds more than hint.

For example, the code below will read two lines as after reading two lines, the number of characters will exceed the hint, 25.

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.




2 Ways to write to a file.

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.



File operation modes.

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.




Moving the cursor within the file.

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
0 is the current cursor location.
2 is the cursor location after seek operation.
2 is the current cursor location as reported by tell operation.
The next three lines are the content of the file returned by read() method. Notice the missing 2 characters which have been skipped due to seek() operation.

If you are skipping characters from the end of the file i.e. when whence=2, offset must be negative.

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



Checking stats of a file.

You can use stats function of os module to check the stats of a file.

import os
print(os.stat("files.py"))
output:

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.



Copying a file.


To copy a file, we can either use 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.



Moving a file.

There are three ways to move a file.

using system method of os module.

os.system("mv source destination")

using rename method of os module.

os.rename("sourcepath", "destination path")

using move method of shutil method.

shutil.move("source", "destination")



Deleting a file.

There are two ways to delete a file.

Using remove method of os module.

os.remove("path")

using system method of os module.

os.system("rm pathname")

You might have noticed by now that 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.

for example, to get the list of all files, directories, and sub-directories in a directory, we can use 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

file read write   0   9166

0 thoughts on 'Read, Write, Tell, Seek, Check Stats, Move, Copy And Delete A File In Python'
Leave a comment:


*All Fields are mandatory. **Email Id will not be published publicly.

SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap