Unit 6 -FileHandling
Unit 6 -FileHandling
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 ")
The absolute path includes the complete directory list required to locate the file.
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().
▪ 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.
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()
▪ 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.
▪ 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.
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
Method Description
close() Close an open file. It has no effect if the file is already closed.
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).
▪ 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.
▪ 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:
▪ Reading Specific Columns: use the multi-axes indexing method called .loc()
▪ 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
▪ 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.
▪ 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.
▪ 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.
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.
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
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