0% found this document useful (0 votes)
17 views

Files

Uploaded by

Daksh Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Files

Uploaded by

Daksh Malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Python File Handling

File handling is a mechanism to save data for further use. Python


supports file handling and allows users to handle files i.e., to read
and write files, along with many other file handling options, to
operate on files.

So far, we've taken input from the console, written it back to the console,
and interacted with the user.

Displaying data in the console may not be enough. Since memory is volatile
and it is impossible to recover the data generated by the program many
times, the displayed data can be very large and only a limited amount of
data can be displayed on the console. Can not be displayed.

File handling plays an important role when data needs to be permanently


stored in a file. A file is a named location on disk that stores related
information. The stored information (non-volatile) can be accessed after the
program exits.

Each line of a file is terminated with a special character, called the EOL or
End of Line characters like comma {,} or newline character. It ends the
current line and tells the interpreter a new one has begun.

Implementing file management is a little longer and more complicated in


other programming languages, but easier and shorter in Python.

Types Of File in Python


There are two types of files in Python and each of them are explained below
in detail:

They are:
 Binary file
 Text file

Binary files in Python


Most of the files that we see in our computer system are called binary files.

In this type of file, there is no terminator for a line and the data is stored
after converting it into machine understandable binary language.
Example:
1. Document files: .pdf, .doc, .xls etc.
2. Image files: .png, .jpg, .gif, .bmp etc.
3. Video files: .mp4, .3gp, .mkv, .avi etc.
4. Audio files: .mp3, .wav, .mka, .aac etc.
5. Database files: .mdb, .accde, .frm, .sqlite etc.
6. Archive files: .zip, .rar, .iso, .7z etc.
7. Executable files: .exe, .dll, .class etc.
8. All binary files follow a specific format. We can open some binary files
in the normal text editor but we can’t read the content present inside
the file. That’s because all the binary files will be encoded in the binary
format, which can be understood only by a computer or machine.
9. For handling such binary files we need a specific type of software to
open it.
10. For Example, You need Microsoft word software to open .doc
binary files. Likewise, you need a pdf reader software to open .pdf
binary files and you need a photo editor software to read the image
files and so on.

Text files in Python


Text files don’t have any specific encoding and it can be opened in normal
text editor itself.

In this type of file, Each line of text is terminated with a special character
called EOL (End of Line), which is the new line character (‘\n’) in python by
default.

Example:
 Web standards: html, XML, CSS, JSON etc.
 Source code: c, app, js, py, java etc.
 Documents: txt, tex, RTF etc.
 Tabular data: csv, tsv etc.
 Configuration: ini, cfg, reg etc.

In Python, files are treated in two modes as text or binary. The file may be in
the text or binary format, and each line of a file is ended with the special
character.

Hence, a file operation can be done in the following order.

o Open a file
o Read or write - Performing operation
o Close the file

File opening modes

Python provides an open() function that accepts two arguments, file name
and access mode in which the file is accessed. The function returns a file
object which can be used to perform various operations like reading, writing,
etc.
Syntax:

The files can be accessed using various modes like read, write, or append.
The following are the details about the access mode to open a file.

S Access Description
N mode

1 r It opens the file to read-only mode. The file pointer exists at the
beginning. The file is by default open in this mode if no access
mode is passed.

2 rb It opens the file to read-only in binary format. The file pointer


exists at the beginning of the file.

3 r+ It opens the file to read and write both. The file pointer exists at
the beginning of the file.

4 rb+ It opens the file to read and write both in binary format. The file
pointer exists at the beginning of the file.

5 w It opens the file to write only. It overwrites the file if previously


exists or creates a new one if no file exists with the same
name. The file pointer exists at the beginning of the file.

6 wb It opens the file to write only in binary format. It overwrites the


file if it exists previously or creates a new one if no file exists.
The file pointer exists at the beginning of the file.

7 w+ It opens the file to write and read both. It is different from r+ in


the sense that it overwrites the previous file if one exists
whereas r+ doesn't overwrite the previously written file. It
creates a new file if no file exists. The file pointer exists at the
beginning of the file.

8 wb+ It opens the file to write and read both in binary format. The file
pointer exists at the beginning of the file.
9 a It opens the file in the append mode. The file pointer exists at
the end of the previously written file if exists any. It creates a
new file if no file exists with the same name.

10 ab It opens the file in the append mode in binary format. The


pointer exists at the end of the previously written file. It creates
a new file in binary format if no file exists with the same name.

11 a+ It opens a file to append and read both. The file pointer remains
at the end of the file if a file exists. It creates a new file if no file
exists with the same name.

12 ab+ It opens a file to append and read both in binary format. The
file pointer remains at the end of the file.

In addition you can specify if the file should be handled as binary or text
mode

"t" - Text - Default value. Text mode

"b" - Binary - Binary mode (e.g. images)

Creating a file

Python allows us to open a file by using 2 methods:


1. using “x” mode
2. using “w” mode

Example 1:
# By using “x”mode
f=open("newdata.txt","x")
Output:
Note: newdata.txt will be an empty file
Example 2:
#by using “w” mode

f=open("latestdata.txt","w")
text="this file is created using 'w' mode"
f.write(text)
Output:

Reading contents of file

There is more than one way to read a file in Python. If you need to extract a
string that contains all characters in the file then we can use file.read().

Example 1:
# To create a file and read it’s contents

Open a new project


Create : data.txt
Write following data:
This is a text file

Create:myfile.py

f=open(“data.txt”,”r”)
print(f.read())

Output:

This is a text file

Example 2:

#To print first 10 characters of file:

f=open("data.txt","r")
print(f.read(10))

Example 3:

#To read contents line by line

data.txt:

This is a text file


1. This is line 1
2. This is line 2
3. This is line 3
4. This is line 4
5. This is line 5

myfile.py

f=open("data.txt","r")
print(f.readline())
print(f.readline())
print(f.readline())

Output:
This is a text file

1. This is line 1

2. This is line 2

Manipulation of file content

Manipulation of file content means that we want to change already existing


data. This can done by 2 methods:

1. opening a file in ”a” mode (Append - will append to the end of the file)
2. opening the file in “w” mode (Write - will overwrite any existing
content)
Note:
In the case of append mode, the contents written will be appended/added
at the end of file while in the case of write mode the previous contents will
be overwritten.
Example:
# using append mode
f=open("data.txt","a")
f.write("\n")
text='''Now some data will be added
6. This is line 6
7.This is line 7
8. This is line 8'''
f.write(text)
Output:

Example:
# using write mode
f=open("data.txt","w")
f.write("\n")
text='''Now some data will be added
6. This is line 6
7.This is line 7
8. This is line 8'''
f.write(text)

Output:
Closing a File
It is a good practice to always close the file when you are done with it. The
close() command terminates all the resources in use and frees the system
of this particular program.

Example 1:

# To close the file when not in use

f=open("data.txt","r")

print(f.read())

f.close()

Example 2:

f=open("data.txt","r")

print(f.read())

f.close()

print(f.read())

Output:

Error # As the file is closed.

Deleting a File

To delete a file, we must import the OS module, and run


its os.remove() function:

Example:

# To delete latestdata.txt

import os

os.remove("latestdata.txt")
Check if File exist

There may be a situation where you want to check whether a file exists or
not before any kind of further processing. For this we can use following code.

Example:

# Check whether latestdata.txt exists or not

import os

if os.path.exists("latestdata.txt"):

print("The file exists...please proceed")

else:

print("The file does not exist")

Output:

The file does not exist

Delete Folder

To delete an entire folder, use the os.rmdir() method:

Example:
import os
os.rmdir("myfolder")

The with statement

The with statement was introduced in python 2.5. The with statement is
useful in the case of manipulating the files. It is used in the scenario where a
pair of statements is to be executed with a block of code in between. The
advantage of using with statement is that it provides the guarantee
to close the file regardless of how the nested block exits.

Syntax:

with open(<file name>, <access mode>) as <file-pointer>:


#statement suite

During the program, if the break, return, or exception occurs in the


nested block of code then it automatically closes the file, we don't
need to write the close() function. It doesn't let the file to corrupt.

Example:

with open("file.txt",'r') as f:
content = f.read();
print(content)

File Pointer positions

A file pointer denotes the position from which the file contents will
be read or written. File handle is also called as file pointer or cursor.

The seek() function sets the position of a file pointer. Python provides us the
seek() method which enables us to modify the file pointer position externally.

Syntax:

<file-ptr>.seek(offset, from)

The seek() method accepts two parameters:

offset: It refers to the new position of the file pointer within the file.

from: It indicates the reference position from where the bytes are to be
moved.
The allowed values for the from argument are: –

 A from value of 0 means from the beginning of the file.


 A from value of 1 uses the current file position
 A from value of 2 uses the end of the file as the reference point.
The default value for the from is the beginning of the file, which is 0

Refer to the below table for clear understanding.

Seek Operation Meaning

f.seek(0) Move file pointer to the beginning of a File

Move file pointer five characters ahead from the beginning


f.seek(5)
of a file.

f.seek(0, 2) Move file pointer to the end of a File

Move file pointer five characters ahead from the current


f.seek(5, 1)
position.

Move file pointer five characters behind from the current


f.seek(-5, 1)
position.

Move file pointer in the reverse direction. Move it to the


f.seek(-5, 2)
5th character from the end of the file

The tell() function returns the current position of a file pointer.

In short, The seek() function sets the position of a file pointer and
the tell() function returns the current position of a file pointer.

Example:
fileptr = open("data.py", "r")

# initially the filepointer is at 0


print("The file pointer is at byte :", fileptr.tell())

# changing the file pointer location to 10.


fileptr.seek(10);

# tell() returns the location of the fileptr.


print("After reading, the file pointer is at:", fileptr.tell())

Output:

Python OS module

The Python os module enables interaction with the operating


system. The os module provides the functions that are involved in file
processing operations like renaming, deleting, etc. It provides us the
rename() method to rename the specified file to a new name. The syntax to
use the rename() method is given below.

Creating the new directory

The mkdir() method is used to create the directories in the current working
directory. The syntax to create the new directory is given below.

Syntax:

mkdir(directory name)
Example:
import os
#creating a new directory with the name new
import os
os.mkdir("New Folder")

Output:
Note: Here, a new folder is created with the name “New Folder”

Renaming a directory

The rename() method is used to change name of directory. The syntax to


rename a directory is given below.

Syntax:

rename(current-name, new-name)

The first argument is the current file name and the second argument is the
modified name. We can change the file name bypassing these two
arguments.

Example:

import os
os.rename("New Folder","Latest Folder")
Output:

Note: “New Folder” is renamed as “Latest Folder”

Get current working directory

This method returns the current working directory.

Syntax:

os.getcwd()

Example:

import os
print(os.getcwd())
Output:
C:\Users\Administrator\PycharmProjects\pythonProject

Changing the current working directory

The chdir() method is used to change the current working directory to a


specified directory.

The syntax to use the chdir() method is given below.

Syntax:
chdir("new-directory")

Example:
import os

print(os.getcwd())
os.chdir("C:\\Users\\IINTM\\Documents")
#It will display the current working directory
print(os.getcwd())

Output:

C:\Users\Administrator\PycharmProjects\pythonProject

C:\Users\IINTM\Documents

The file related methods

The file object provides the following methods to manipulate the files on
various operating systems.

SN Method Description

1 file.close() It closes the opened file. The file once closed, it


can't be read or write anymore.

2 File.fush() It flushes the internal buffer.

3 File.fileno() It returns the file descriptor used by the


underlying implementation to request I/O from
the OS.

4 File.isatty() It returns true if the file is connected to a TTY


device, otherwise returns false.

5 File.next() It returns the next line from the file.

6 File.read([size]) It reads the file for the specified size.


7 File.readline([size]) It reads one line from the file and places the file
pointer to the beginning of the new line.

8 File.readlines([sizehint It returns a list containing all the lines of the file.


]) It reads the file until the EOF occurs using
readline() function.

9 File.seek(offset[,from) It modifies the position of the file pointer to a


specified offset with the specified reference.

10 File.tell() It returns the current position of the file pointer


within the file.

11 File.truncate([size]) It truncates the file to the optional specified size.

12 File.write(str) It writes the specified string to a file

13 File.writelines(seq) It writes a sequence of the strings to a file.

You might also like