0% found this document useful (0 votes)
4 views57 pages

Unit 6 -FileHandling

The document provides a comprehensive overview of file handling in Python, covering the need for file systems, basic operations, file modes, and methods for reading and writing files. It explains how to open, close, and manipulate files, as well as the distinction between text and binary I/O. Additionally, it introduces the CSV module for handling CSV files and the OS module for interacting with the operating system's file system.

Uploaded by

Ritesh bhusara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views57 pages

Unit 6 -FileHandling

The document provides a comprehensive overview of file handling in Python, covering the need for file systems, basic operations, file modes, and methods for reading and writing files. It explains how to open, close, and manipulate files, as well as the distinction between text and binary I/O. Additionally, it introduces the CSV module for handling CSV files and the OS module for interacting with the operating system's file system.

Uploaded by

Ritesh bhusara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

FILE HANDLING

Roadmap
▪ Need of File System
▪ Basics of File Handling
▪ Modes of Files
▪ File Methods
▪ Examples
▪ Case Study
CO2 : To explore the contents of files ,directories and
text processing with python.
File Handling in Python 2
Understand Program!!
name=input("Enter your name ")

math=float(input("Enter your marks in Math"))

physics=float(input("Enter your marks in Physics"))

chemistry=float(input("Enter your marks in Chemistry"))

rollno=int(input("Enter your Roll no"))

print ( "Welcome ",name )

print ("Your Roll no is ",rollno )

print ("Marks in Maths is ",math )

print ("Marks in Physics is ",physics )

print ("Marks in Chemistry is ",chemistry )

print ("Average marks is ",(math+physics+chemistry)/3 )

File Handling in Python 3


What is 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).
▪ To store data temporarily and permanently, we use files. A file is the
collection of data stored on a disk in one unit identified by filename.
▪ 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.
▪ Open a file
▪ Read or write (perform operation)
▪ Close the file

File Handling in Python 4


Basics of File Handling

File Handling in Python 5


Modes of Files
❑ There are different modes of file in which it can be opened.
They are mentioned in the following table.
❑ A File can be opened in two modes:
1) Text Mode.
2) Binary Mode.

File Handling in Python 6


File Path
A file path defines the location of a file or folder in the computer system.
There are two ways to specify a file path.
1.Absolute path: which always begins with the root folder
2.Relative path: which is relative to the program's current working directory

The absolute path includes the complete directory list required to locate the file.

For example, /user/Pynative/data/sales.txt is an absolute path to discover the


sales.txt.

All of the information needed to find the file is contained in the path string.

After the filename, the part with a period(.) is called the file's extension, and that tells us
the type of file.
Here, project.pdf is a pdf document.
File Handling in Python 7
Open a File
1) Opening a File: Before working with Files you have to open the
File. To open a File, Python built in function open() is used. It
returns an object of File which is used with other functions.
Having opened the file now you can perform read, write, etc.
operations on the File.
Syntax:
fileobj=open(filename , mode)
▪ filename:It is the name of the file which you want to access.
▪ mode:It specifies the mode in which File is to be opened. There
are many types of mode. Mode depends the operation to be
performed on File. DefaultFileaccess mode is read.
Handling in Python 8
Create a File
We don’t have to import any module to create a new file. We can create a file using the built-in
function open().

File Handling in Python 9


Opening a File with Relative Path

▪ A relative path is a path that starts with the working directory or the
current directory and then will start looking for the file from that
directory to the file name.

File Handling in Python 10


File open() function

File Handling in Python 11


Parameter Description

This parameter value gives the pathname (absolute or relative to the current working directory) of the
file
file to be opened.

This is the optional string that specifies the mode in which a file will be opened. The default value
mode
is 'r' for reading a text file. We can discuss the other modes in the later section.

This is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in
buffering binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size
in bytes of a fixed-size chunk buffer.

This is the name of the encoding used to decode or encode the file. The default one is platform
encoding
dependant.

errors These are optional string denotes how the standard encoding and decoding errors have to be handled.

This is the parameter that indicates how the newline mode works (it only applies to text mode). It can
newline
be None, '', '\n', '\r', and '\r\n'.

This parameter indicates whether to close a file descriptor or not. The default value is True.
closefd If closefd is False and a file descriptor rather than a filename was given, the underlying file descriptor
will be kept open when the file is closed.
File Handling in Python 12
File Handling in Python 13
Close a File
2) Closing a File:Once you are finished with the operations on
File at the end you need to close the file. It is done by the
close() method. close() method is used to close a File.
Syntax:
fileobject.close()

File Handling in Python 14


Modes of Files

File Handling in Python 15


File Attributes :Metadata about working file

▪ 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 −
▪ Attribute ▪ Description
▪ Name ▪ Returns the name of the file.
▪ Mode ▪ Returns the mode in which file is being opened.

▪ Closed ▪ Returns Boolean value. True, in case if file is


closed else false.

File Handling in Python 16


Write a File
3) Writing to a File:write() method is used to write a string into
a file.
▪ Syntax:
▪ Fileobject.write(string str)
file = open(“testfile.txt”,”w”)
file.write(“Hello World”)
file.write(“This is our new text file”)
file.write(“and this is another line.”)
file.write(“Why? Because we can.”)
file.close()
File Handling in Python
17
▪ write multiple lines to a file at once
>>> fh = open(“hello.txt”,”w”)
>>> lines_of_text = [“One line of text here”, “and another line
here”, “and yet another here”, “and so on and so forth”]
>>> fh.writelines(lines_of_text)
>>> fh.close()

▪ To append a file
>>> fh = open(“hello.txt”, “a”)
>>> fh.write(“We Meet Again World”)
>>> fh.close
File Handling in Python 18
Read a File
❑ Reading from a File:read() method is used to read data from
the File.
❑ Syntax:
❑ fileobject.read(value)
❑ here, value is the number of bytes to be read. In case, no
value is given it reads till end of file is reached.

File Handling in Python 19


Variations in Read Mode
1) read() or read(4)
Use the read(size) method to read in size number of data. If size parameter is
not specified, it reads and returns up to the end of the file.
Eg. file = open(“testfile.text”, “r”)
print file.read() #till end of file
print file.read(5) #only first 5 bytes

2)readline() or readline(3)
Use readline() method to read individual lines of a file. This method reads
only the first line of the file.
3)readlines()
reads all the lines and put them in 'list'(between these [ ]) and displays them
File Handling in Python 20
Looping over a file object
▪ read – or return – all the lines from a file in a more memory efficient, and fast
manner
Eg. file = open(“testfile.txt”, “r”)
for line in file:
print line

▪ use the with statement to open a file


Eg. with open(“testfile.txt”) as f:
for line in f:
print line

▪ Splitting Lines in a Text File


Eg. with open(“hello.text”, “r”) as f:
data = f.readlines()
for line in data:
words = line.split()
print words File Handling in Python 21
File Methods

Method Description

close() Close an open file. It has no effect if the file is already closed.

Separate the underlying binary buffer from the TextIOBaseand


detach()
return it.

fileno() Return an integer number (file descriptor) of the file.

flush() Flush the write buffer of the file stream.

isatty() Return True if the file stream is interactive.


File Handling in Python 22
File Methods
Method Description
Read atmost n characters form the file. Reads till end of file if it is negative
read(n)
or None.

readable() Returns True if the file stream can be read from.

readline(n=-1) Read and return one line from the file. Reads in at most nbytes if specified.

Read and return a list of lines from the file. Reads in at most n bytes/characters if
readlines(n=-1)
specified.

seek(offset,fro Change the file position to offset bytes, in reference to from (start, current,
m=SEEK_SET) end).

seekable() Returns True if the file stream supports random access.


File Handling in Python 23
File Methods
Method Description

tell() Returns the current file location.

Resize the file stream to size bytes. If size is not specified,


truncate(size=None)
resize to current location.

writable() Returns True if the file stream can be written to.

Write string s to the file and return the number of characters


write(s)
written.

writelines(lines) Write a list of lines to the file.

File Handling in Python 24


I/O Module

▪ The io module provides Python’s main facilities for dealing for various types of
I/O.
▪ There are three main types of I/O: text I/O, binary I/O, raw I/O.
▪ These are generic categories, and various backing stores can be used for each of
them.
▪ Concrete objects belonging to any of these categories will often be
called streams;
▪ Another common term is file-like objects. Independently of its category, each
concrete stream object will also have various capabilities: it can be read-only,
write-only, or read-write.
▪ It can also allow arbitrary random access (seeking forwards or backwards to any
location), or only sequential access (for example in the case of a socket or pipe).
▪ All streams are careful about the type of data you give to them. For example
giving a str object to the write() method of a binary stream will raise a TypeError
. So will giving a bytes object to the write() method of a text stream.
File Handling in Python 25
▪ This module is a part of the standard library, so there’s no need to install it separately
using pip.

File Handling in Python 26


File Handling in Python 27
File Handling in Python 28
File Handling in Python 29
Text I/O
▪ Text I/O expects and produces str objects. This means that whenever
the backing store is natively made of bytes (such as in the case of a file),
encoding and decoding of data is made transparently as well as optional
translation of platform-specific newline characters.
▪ The easiest way to to create a text stream is with open(), optionally
specifying an encoding:

File Handling in Python 30


Binary I/O

▪ Binary I/O (also called buffered I/O) expects and produces bytes
objects. No encoding, decoding, or newline translation is performed.
This category of streams can be used for all kinds of non-text data,
and also when manual control over the handling of text data is
desired.
▪ The easiest way to create a binary stream is with open() with ‘b’ in
the mode string:

File Handling in Python 31


Raw I/O

▪ Raw I/O (also called unbuffered I/O) is generally used as a


low-level building-block for binary and text streams; it is rarely
useful to directly manipulate a raw stream from user code.
Nevertheless, you can create a raw stream by opening a file in
binary mode with buffering disabled:

File Handling in Python 32


Encoding
❑ Unlike other languages, the character 'a' does not imply the
number 97 until it is encoded using ASCII (or other equivalent
encodings).
❑ Moreover, the default encoding is platform dependent. In
windows, it is 'cp1252' but 'utf-8' in Linux.
❑ So, we must not also rely on the default encoding or else our
code will behave differently in different platforms.
❑ Hence, when working with files in text mode, it is highly
recommended to specify the encoding type.
❑ f = open("test.txt",mode = 'r',encoding = 'utf-8')
File Handling in Python 33
Processing CSV Data : using pandas

▪ Input as CSV File


▪ csv file is a text file in which the values in the columns are separated by a comma.

▪ Reading a CSV File


▪ read_csv function of the pandas library is used read the content of a CSV file

File Handling in Python 34


Processing CSV Data : using pandas

▪ Reading Specific Rows

▪ Reading Specific Columns: use the multi-axes indexing method called .loc()

File Handling in Python 35


Processing CSV Data : using pandas

▪ Reading Specific Columns and Rows

File Handling in Python 36


Working with the CSV Module

▪ The CSV module includes all the necessary functions built in. They are:
▪ csv.reader
▪ csv.writer
▪ csv.register_dialect
▪ csv.unregister_dialect
▪ csv.get_dialect
▪ csv.list_dialects
▪ csv.field_size_limit

▪ CSV Sample File

File Handling in Python 37


Working with the CSV Module

▪ Reading CSV Files

▪ Extracting information from a CSV file

File Handling in Python 38


Working with the CSV Module

▪ Writing to CSV Files

File Handling in Python 39


▪ Modifying existing rows

File Handling in Python 40


OS Module in Python

▪ The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s
standard utility modules. This module provides a portable way of using operating system-dependent functionality.
The *os* and *os.path* modules include many functions to interact with the file system.
▪ Handling the Current Working Directory
▪ Consider Current Working Directory(CWD) as a folder, where the Python is operating. Whenever the files are
called only by their name, Python assumes that it starts in the CWD which means that name-only reference will be
successful only if the file is in the Python’s CWD.
Note: The folder where the Python script is running is known as the Current Directory. This is not the path where
the Python script is located.
▪ To get the location of the current working directory os.getcwd() is used.

import os
cwd = os.getcwd()
print("Current working directory:", cwd) File Handling in Python 41
Changing the Current working directory
▪To change the current working directory(CWD) os.chdir() method is used. This method changes the CWD to a
specified path. It only takes a single argument as a new directory path.

File Handling in Python 42


Creating a Directory

▪ There are different methods available in the OS module for creating a directory. These are –
• os.mkdir()
• os.makedirs()
Using os.mkdir()
▪ os.mkdir() method in Python is used to create a directory named path with the specified numeric mode. This
method raises FileExistsError if the directory to be created already exists.
Using os.makedirs()
▪ os.makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if
any intermediate-level directory is missing, os.makedirs() method will create them all.

File Handling in Python 43


File Handling in Python 44
Here, setting mode = 0o666 allows the user to perform read and write file operations within the created
directory.

File Handling in Python 45


▪ File permission 755 means that the directory has the default permissions
-rwxr-xr-x (represented in octal notation as 0755).

File Handling in Python 46


Listing out Files and Directories with Python

▪ os.listdir() method in python is used to get the list of all files and directories in the specified
directory. If we don’t specify any directory, then list of files and directories in the current
working directory will be returned.

File Handling in Python 47


Deleting Directory or Files using Python

OS module proves different methods for removing directories and files in Python. These are –
•Using os.remove()
•Using os.rmdir()
Using os.remove()
▪os.remove() method in Python is used to remove or delete a file path. This method can not remove or delete a
directory. If the specified path is a directory then OSError will be raised by the method.
Using os.rmdir()
▪os.rmdir() method in Python is used to remove or delete an empty directory. OSError will be raised if the specified
path is not an empty directory.

File Handling in Python 48


import os
Deleting File
file = 'file1.txt'
location = "D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil/"
path = os.path.join(location, file)
os.remove(path)

File Handling in Python 49


Deleting Directory

File Handling in Python 50


Commonly Used Functions
Sr Function Description
No
1 os.name This function gives the name of the operating system dependent module imported. The following
names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’
2 os.error All functions in this module raise OSError in the case of invalid or inaccessible file names and paths,
or other arguments that have the correct type, but are not accepted by the operating system. os.error
is an alias for built-in OSError exception.
3 os.popen( This method opens a pipe to or from command. The return value can be read or written depending
) on whether the mode is ‘r’ or ‘w’.
os.popen(command[, mode[, bufsize]])
4 os.close() Close file descriptor fd. A file opened using open(), can be closed by close()only. But file opened
through os.popen(), can be closed with close() or os.close(). If we try closing a file opened with
open(), using os.close(), Python would throw TypeError.
5 os.rename A file old.txt can be renamed to new.txt, using the function os.rename(). The name of the file
() changes only if, the file exists and the user has sufficient privilege permission to change the file.
6 os.remove Using the Os module we can remove a file in our system using the remove() method. To remove a
() file we need to pass the name of the file as a parameter.
7 os.path.ex This method will check whether a file exists or not by passing the name of the file as a parameter.
ists() OS module has a sub-module named PATH by using which we can perform many more functions.
File Handling in Python 51
8 os.path.ge In this method, python will give us the size of the file in bytes. To use this method we need to pass
OS module
Sr.No Function Name Description

1 os.getcwd() The getcwd() method displays the current working directory.

2 os.mkdir("newdir") use the mkdir() method of the os module to create directories in the
current directory.
3 os.chdir("newdir") chdir() method to change the current directory. The chdir() method
takes an argument, which is the name of the directory that you want to
make the current directory.
4 os.remove(file_nam remove() method to delete files by supplying the name of the file to be
e) deleted as the argument.
5 os.rename(current_f The rename() method takes two arguments, the current filename and
ile_name, the new filename.
new_file_name)
6 os.listdir() Display files in current working directory
File Handling in Python 52
Example 1:Find Hash of File

▪ This program will help to test the learner understanding


of Python Functions ,Python User-defined Functions and
Python File I/O
▪ Hash functions take an arbitrary amount of data and return a
fixed-length bit string. The output of the function is called the
digest message.
▪ They are widely used in cryptography for authentication
purposes. There are many hashing functions like MD5,
SHA-1 etc.

File Handling in Python 53


Example 2 : Find the Size (Resolution) of a Image

❑ This program will help to test the learner understanding of Python


Functions ,Python User-defined Functions and Python File I/O
❑ JPEG (pronounced "jay-peg") stands for Joint Photographic Experts
Group. It is one of the most widely used compression techniques for
image compression.
❑ Most of the file formats have headers (initial few bytes) which
contain useful information about the file.
❑ For example, jpeg headers contain information like height, width,
number of color (grayscale or RGB) etc. In this program, we find the
resolution of a jpeg image reading these headers, without using any
external library.
File Handling in Python 54
Practical Programs in Syllabus

4. Exploring Files and directories


▪ a. Python program to append data to existing file and then
display the entire file.
▪ b. Python program to count number of lines, words and
characters in a file.
▪ c. Python program to display file available in current directory

File Handling in Python 55


Case Study: Address Book

Now let us use object IO to create a useful project for storing and
viewing an address book. The user interface of the program is shown
below. The Add button stores a new address at the end of the file. The
First, Next, Previous, and Last buttons retrieve the first, next, previous,
and last addresses from the file, respectively.

56
File Handling in Python 57

You might also like