PYTHON FILE HANDLING
What is a file?
File is a named location on disk to store related information. It is used to permanently store data
in a non-volatile memory (e.g. hard disk).
Since, random access memory (RAM) is volatile which loses its data when computer is turned off,
we use files for future use of the data.
When we want to read from or write to a file we need to open it first. When we are done, it needs
to be closed, so that resources that are tied with the file are freed.
Hence, in Python, a file operation takes place in the following order.
i. Open a file which returns a file handle.
ii. Use the handle to perform read or write action.
iii. Close the file handle.
In python, the file should be handled as binary or text mode
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode (e.g. images)
1) Open A File in Python:
To read or write to a file, you need to open it first. To open a file in Python, use its built
open () function. This function returns a file object i.e. a handle. You can use it to read or
modify the file.
Python open () File Method:
file object = open (file_name [, access_mode] [, buffering])
Below are the parameter details.
<access_mode> : It’s an integer representing the file open mode e.g. read, write, append, etc. It’s
an optional parameter.
By default, it is set to read-only <r>. In this mode, we get data in text form after reading
from the file.
On the other hand, binary mode returns bytes. It’s preferable for accessing the non-text
files like an image or the Exe files.
See the table in the next section. It lists down the available access modes.
<buffering> : The default value is 0 which means buffering won’t happen. If the value is 1, then
line buffering will take place while accessing the file. If it’s greater than 1, then the buffering
action will run as per the buffer size. In the case of a negative value, the default behavior is
considered.
<file_name>: It’s a string representing the name of the file you want to access.
File Open Modes in Python:
Modes Description
<r> It opens a file in read-only mode while the file offset stays at the root.
It opens a file in (binary + read-only) modes. And the offset remains at the root
<rb>
level.
It opens the file in both (read + write) modes while the file offset is again at the
<r+>
root level.
It opens the file in (read + write + binary) modes. The file offset is again at the
<rb+>
root level.
It allows write-level access to a file. If the file already exists, then it’ll get
<w>
overwritten. It’ll create a new file if the same doesn’t exist.
Use it to open a file for writing in binary format. Same behavior as for write-
<wb>
only mode.
It opens a file in both (read + write) modes. Same behavior as for write-only
<w+>
mode.
It opens a file in (read + write + binary) modes. Same behavior as for write-only
<wb+>
mode.
It opens the file in append mode. The offset goes to the end of the file. If the file
<a>
doesn’t exist, then it gets created.
<ab> It opens a file in (append + binary) modes. Same behavior as for append mode.
<a+> It opens a file in (append + read) modes. Same behavior as for append mode.
It opens a file in (append + read + binary) modes. Same behavior as for append
<ab+>
mode.
Examples: open ( )
# open a text file called "TestingText" in read-only mode
file_example = open ("TestingText.txt") # OR
file_example = open ("TestingText.txt", ”r”)
# opens the file named “workfile” in writing mode so that we can make changes to it.
f = open (“workfile”, ”w”)
print(f)
Note: Something to keep in mind is to always make sure that both the file name and the path
given are correct. If either is incorrect or doesn't exist, the error FileNotFoundError will be
thrown, which needs to then be caught and handled by your program to prevent it from
crashing.
try:
file_example = open ("F:\\Directory\\AnotherDirectory\\tests\\TestingText.txt")
except IOError:
print("An error was found. Either path is incorrect or file doesn't exist!")
finally:
print("Terminating process!")
2) Writing to Files with write ():
In order to write into a file in Python, we need to open it in write 'w', append 'a' or
exclusive creation 'x' mode.
We need to be careful with the 'w' mode as it will overwrite into the file if it already
exists. All previous data are erased.
Examples: write ( )
file_example2 = open (“TestingTextTwo.txt", "w")
file_example2.write("This is a test. Enjoy it!\n") # '\n' is for change line.
file_example2.write("Another thing to know is doing it slowly.\n")
file_example2.write("One by one. Yay!")
# If several lines need to be written, the sub-function writelines() could be used instead:
listOfThingsToSay = ["I like things like: \n", "Ice Cream\n", "Fruits\n", "Movies\n", "Anime\n",
"Naps\n", "Jerky\n"]
file_example2 = open ("TestingTextTwo.txt", "w")
file_example2.writelines(listOfThingsToSay)
#To be able to use print() function, the mode needs to be set as w+, which allows reading as
well as writing.
listOfThingsToSay = ["I like things like: \n", "Ice Cream\n", "Fruits\n", "Movies\n", "Anime\n",
"Naps\n", "Jerky\n"]
file_example2 = open ("F:\\Directory\\AnotherDirectory\\tests\\TestingTextTwo.txt", "w")
file_example2.writelines(listOfThingsToSay)
3) Adding to Files with append ():
This function acts similar to the write () function, however, instead of overwriting the file, the
append () function appends content to the existing file.
# If a text file named "TestingTextThree" contains the following information:
Some essential things are missing in life and should not be avoided.
# In order to append new text, the following code could be used:
listOfThingsDo = ["You need at least to: \n", "Eat fried Ice Cream\n", "Go to Disney\n", "Travel
to the moon\n", "Cook Pineapple Pizza\n", "Dance Salsa\n"]
file_example3 = open ("TestingTextThree.txt", "a+")
file_example3.writelines(listOfThingsToDo)
for newline in file_example3
print(newlines)
The output will be as follows:
Some essential things are missing in life and should not be avoided.
You need at least to:
Eat fried Ice Cream
Go to Disney
Travel to the moon
Cook Pineapple Pizza
Dance Salsa
4) Reading from Files with read ()
Python contains 3 functions to read files: read (), readline (), and readlines (). The last two
functions are merely helper functions that make reading certain types of files easier.
For the examples that will be used, "TestingText.txt" contains the following text:
Hello, world! Python is the way to coding awesomeness.
If you don't believe me, try it on your own.
Come, you will enjoy the Dark Side. We have cookies!
#The read method is used as follows:
file_example = open ("TestingText.txt", "r")
print(file_example.read())
The output will be as follows:
Hello, world! Python is the way to coding awesomeness.
If you don't believe me, try it on your own.
Come, you will enjoy the Dark Side. We have cookies!
Note: Special characters may not be read correctly using the read function. To read special
characters correctly, you can pass the encoding parameter to read () function and set its value to
utf8 as shown below:
file_example = open ("TestingText.txt", "r", encoding="utf8" ) # To read special characters
correctly
# In the case of a text file, this will be the number of characters returned.
file_example = open ("TestingText.txt", "r")
print(file_example.read(8))
The output will be as follows:
Hello, w
# The helper-function readline () behaves in a similar manner, but instead of returning the whole
text, it will return a single line.
file_example = open ("TestingText.txt", "r")
print(file_example.readline()) # This would return the first line of the file
print(file_example.readline(5))
In the script above, the first print () statement returns the first line and inserts a blank line in the
output console. The next print() statement is separated from the previous line with a blank line
and starts on a new line as shown in the output:
OutPut:
Hello world! Python is the way to coding awesomeness.
If yo
Finally, the helper-function readlines () reads all of the text and splits them in to lines for easy
reading. Take a look at the following example:
print(file_example.readlines())
The output from this code will be:
Hello world! Python is the way to coding awesomeness. If you don't believe me, try it on your
own. Come, you will enjoy the Dark Side. We have cookies!
Keep in mind that the readlines() function is considered to be much slower and more
inefficient than the read() function, without not many benefits.
One good alternative to this is using a loop that will function much smoother and faster:
[example]
file_example = open ("TestingText.txt", "r")
for lines in file_example:
print(lines)
5) Looping Over A File Object in Python:
When you want to read – or return – all the lines from a file in a more memory efficient, and fast
manner, you can use the loop over method. The advantage to using this method is that the related
code is both simple and easy to read.
# demo.txt contain following information:
Hello World
This is our new text file
and this is another line.
Why? Because we can.
file = open(“demo.txt”, “r”)
for line in file:
print (line)
# This will return(output):
Hello World
This is our new text file
and this is another line.
Why? Because we can.
6) Closing Opened Files with close ():
The close () function clears the memory buffer and closes the file. This means that we'll no longer
be able to read from the file, and we'll have to re-open it if we want to read from it again.
# Using the previously used sample code, this function is used as follows:
file_example = open ("TestingText.txt", "r")
print(file_example.read())
file_example.close() # to close
7) The file Object Attributes :
Once a file is opened and you have one file object, you can get various information related to that
file.
Here is a list of all attributes related to file object −
Sl.No. Attribute & Description
1 file.closed # Returns true if file is closed, false otherwise.
2 file.mode #Returns access mode with which file was opened.
3 file.name #Returns name of the file.
4 file.softspace #Returns false if space explicitly required with print, true otherwise.
8) FILE POSITIONS :
The tell() method tells you the current position within the file; in other words, the next read
or write will occur at that many bytes from the beginning of the file.
The seek(offset[, from]) method changes the current file position. The offset argument
indicates the number of bytes to be moved. The from argument specifies the reference
position from where the bytes are to be moved.
If from is set to 0, it means use the beginning of the file as the reference position and 1
means use the current position as the reference position and if it is set to 2 then the end of
the file would be taken as the reference position.
Example
Let us take a file foo.txt, which we created above.
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print ("Read String is : ", str)
# Check current position
position = fo.tell();
print ("Current file position : ", position)
# Reposition pointer at the beginning once again
position = fo.seek(0, 0);
str = fo.read(10);
print ("Again read String is : ", str)
# Close opend file
fo.close()
9) OS module
Renaming and Deleting Files
Python os module provides methods that help you perform file-processing operations, such as
renaming and deleting files.
To use this module you need to import it first and then you can call any related functions.
The rename () Method
The rename () method takes two arguments, the current filename and the new
filename.
Syntax
os.rename (current_file_name, new_file_name)
Example
Following is the example to rename an existing file test1.txt –
import os
# Rename a file from test1.txt to test2.txt
os.rename( "test1.txt", "test2.txt" )
The remove() Method: Use the remove() method to delete files by supplying the name of
the file to be deleted as the argument.
Syntax
os.remove(file_name)
Example
Following is the example to delete an existing file test2.txt –
import os os.remove("text2.txt")
# Delete file test2.txt
10) Python File Handling: File Object Methods
Since the Python’s open() method returns an object which we call as the file handle. So, Python
adds a no. of functions that you can call using this object.
*** File1 as file handle
Function Description
File1.close() Close the file. You need to reopen it for further access.
Flush the internal buffer. It’s same as the <stdio>’s
File1.flush()
<fflush()> function.
File1.fileno() Returns an integer file descriptor.
File1.isatty() It returns true if file has a <tty> attached to it.
File1.next() Returns the next line from the last offset.
File1.read(size) Reads the given no. of bytes. It may read less if EOF is hit.
It’ll read an entire line (trailing with a new line char) from
File1.readline(size)
the file.
It calls the <readline()> to read until EOF. It returns a list of
File1.readlines(size_hint) lines read from the file. If you pass <size_hint>, then it
reads lines equalling the <size_hint> bytes.
File1.seek(offset[, from]) Sets the file’s current position.
File1.tell() Returns the file’s current position.
Truncates the file’s size. If the optional size argument is
File1.truncate(size)
present, the file is truncated to (at most) that size.
File1.write(string) It writes a string to the file. And it doesn’t return any value.
Writes a sequence of strings to the file. The sequence is
File1.writelines(sequence) possibly an iterable object producing strings, typically a list
of strings.