Home » Python Tutorial » Python File Handling

Python File Handling


Python File HandlingBefore we move into the topic “Python File Handling”, let us try to understand why we need files? So far, we have been receiving the input data from the console and writing the output data back to the console. The console only displays a limited amount of data. Hence we don’t have any issues if the input or output is small. What if the output or input is too large?

We use files when we have large data as input or output.  A file is nothing but a named location on disk which stores data. Files are also used to store data permanently since it stores data on non-volatile memory.

Python File Handling: File operation:

In Python, the file operation follows the below order:

  • Open a file
  • Read or write a file
  • Close a file

Open a file:

Before performing any operations like read or write on a file, the first thing we need to do is open a file. Python provides an in-built function open() to open a file. The open function accepts two parameters: the name of the file and the access mode. The access mode specifies what operation we are going to perform on a file whether it is read or write. The open() function in turn returns a file object/handle, with which we can perform file operations based on the access mode.

Syntax:

file_object=open(filename, access_mode)

Example:

file_object=open("test.txt") # when file is in the current directory
file_object=open("C:\User\Desktop\test.txt") # specify full path when file is in different directory

If we don’t specify the reading the mode, the default mode is read ‘r’.

Access modes:

Let’s see what are the other access modes.

Access ModeDescription
rread-only mode. The pointer is at the beginning of the file.
rbread-only mode in binary format. The pointer is at the beginning of the file.
r+Allow both read and write operation. The pointer is at the beginning of the file.
rb+Allow both read and write operation in binary format. The pointer is at the beginning of the file.
wwrite-only mode. Overwrites the file if exists already else creates a new one. The pointer is at the beginning of the file.
wbwrite-only mode in binary format. Overwrites the file if exists already else creates a new one. The pointer is at the beginning of the file.
w+Allows both read and write operation. w+ overwrites if the files exists already whereas r+ does not the overwrite the existing one. The pointer is at the beginning of the file.
wb+Allows both read and write operation in binary format. wb+ overwrites if the files exists already whereas rb+ does not the overwrite the existing one. The pointer is at the beginning of the file.
aappend mode. If the file exists, then pointer will be at the end of the file. Else it creates a new file.
abappend mode in binary format. If the file exists, then pointer will be at the end of the file. Else it creates a new file.
a+Allows both append and read operation. If the file exists, then pointer will be at the end of the file. Else it creates a new file.
ab+Allows both append and read operation in binary format. If the file exists, then pointer will be at the end of the file. Else it creates a new file.
READ  Python Keywords

Close a file:

We need to close the file once we are done with performing operations on a file. When we close a file, it will free up all the resources that are tied with the file. We use the inbuilt method close() to close a file. Even though there is Python’s garbage collector for close up, we cannot rely on it for closing the file.

file_object=open("test.txt")
file_object.close()

While we are doing some file operation, if there is an exception, then the code terminates without closing the file. Hence it’s good practice to have a try-finally block.

try:
    file_object=open("test.txt")
    # file operations
finally:
    file_object.close()

with statement:

The advantage of using the ‘with’ statement is that it automatically closes the file even in case of exception. We don’t need a close() function if we use ‘with’ statement. It’s done internally. When working with files, its always good practice to use ‘with’ statement.

with open('file.txt','r') as f:
    #perform file operations

Write to a file:

We use the write() function to write to a file using one of these two access modes:

‘w’: Creates a new file and write to it. The file pointer is at the beginning of the file. If the file exists already, it overwrites the existing file.

‘a’: Append to the existing file. The file pointer is at the end of the file. If there is no file, creates a new one.

Example- with ‘w’:

with open('file.txt','w') as f:
    f.write(' She dissimilar was favourable unreserved nay expression contrasted saw. Past her find she like bore pain open. Shy lose need eyes son not shot')

And the output is :

READ  Python For loop

Python File Handling

Example- with ‘a'(appends to the existing file):

with open('file.txt','a') as f:
    f.write('Jennings removing are his eat dashwood. Middleton as pretended listening he smallness perceived. Now his but two green spoil drift. ')

Python File Handling

Example- with ‘a'(creates a new file and writes to it):

with open('file1.txt','a') as f:
    f.write('Jennings removing are his eat dashwood. Middleton as pretended listening he smallness perceived. Now his but two green spoil drift. ')

Python File Handling

We can include a new line character to write to a new line.

with open('file.txt','w') as f:
    f.write('First line\n')
    f.write('Second line\n')
    f.write('Third line\n')

Read from a file:

Python has read() method to read from a file. The file pointer is at the beginning of the file.

Syntax:

file_object.read(<count>)

count- specifies the number of bytes to be read from the file starting from the beginning of the file. It is an optional parameter. If count is not given, then it reads the entire file.

Example- read entire file:

with open('file.txt','r') as f:
    print(f.read())
First line
Second line
Third line

Example- read the specific number of bytes:

with open('file.txt','r') as f:
    print(f.read(8))
First li

tell() and seek():

Let’s see an example before we look into how the tell() and seek() method works.

with open('file.txt','r') as f:
    print(f.read(8))
    print(f.read())
First li
ne
Second line
Third line

f.read(8): reads the first 8 bytes and sets the current file cursor to 9th byte. That’s why, when we read the whole file, it reads from the 9th bytes.

The tell() returns the file cursor position and the seek() method changes the cursor position.

with open('file.txt','r') as f:
    print(f.read(8))
    print(f.tell())
    print(f.seek(0)) #Takes the file cursor to initial position.
    print(f.read())
First li
8
0
First line
Second line
Third line

Read using for loop:

We can read a file line by line using for loop.

with open('file.txt','r') as f:
    for line in f:
        print(line)
First line

Second line

Third line

readline() and readlines() function:

The readline() function reads the individual lines of a file including the new line character. When it reaches the end of the file, returns an empty value.

f= open('file.txt','r')
line_1=f.readline()
line_2=f.readline()
line_3=f.readline()
line_4=f.readline()
print(line_1)
print(line_2)
print(line_3)
print(line_4)
'First line\n'

'Second line\n'

'Third line\n'

The readlines() function returns the remaining lines of a file. Like readline() function, when it reaches the end of the file, returns an empty value.

f= open('file.txt','r')
line_1=f.readline()
lines=f.readlines()
print(line_1)
print(lines)
'First line\n'

["'Second line\\n'\n", "'Third line\\n'\n"]

Python File Handling: File Methods:

In addition to the above methods, there are other methods to manipulate the files.

READ  Python Lambda
MethodDescription
close()Closes a file if it is open. Does nothing if the file is already closed.
detach()Separates the underlying binary buffer from the "TextIOBase" and returns it.
fileno()Returns the file descriptor of the file which is an integer
flush()Flushes the write buffer of the file stream.
isatty()Returns True if the file stream is interactive.
read(n) Reads 'n' characters from the file. If n is negative or None, reads the entire file.
readable()Returns True if the file stream can be read from.
readlines(n=-1)Reads and returns a list of lines from the file. Reads 'n' bytes 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 position.
truncate(size=None)Resizes the file stream to 'size' bytes. When size is not specified, resizes to current location.
writable()Returns True if the file stream can be written to.
write(s)Writes the string 's' to the file and returns the number of characters written.
writelines(lines)Writes a list of lines to the file.
Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions
Core Java Interview Questions

AD Blocker Detected !

Advertisements help running this website for free.

To view the content please disable AdBlocker and refresh the page.

Wait !!!

You can Crack Technical Interviews of Companies like Amazon, Google, LinkedIn, Facebook, PayPal, Flipkart, etc

Abhishek

Abhishek was able to crack Microsoft after practicing questions from TutorialCup