File Handling

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Understanding File Name and File Path

File is a collection of data that is stored externally on a storage media, such as a hard drive,
SSD, or other storage device. It is not stored in the computer's RAM.

Every file has two attributes: File Name - name of the file
File Path - location of the file

File name consists of two parts separated by a '.' (dot) e.g. sample1.txt
Name - the name of the file (sample1 in sample1.txt)
Extension - the type of the file (txt in sample1.txt) which means it is a text file containing
printable characters. The file extension indicates the type of file and the default program to
open it. For instance, a .txt file can be opened with Notepad, .c files with a C compiler,
and .exe files by the operating system.
File path is the sequence of directories that specifies the location of a file.
for example: C:\Users\rsari\Desktop\Python\(This is the absolute path).
The path can be relative to the current directory which is the default directory.
Faculty Name: Pragya Agarwal Programe Name: B.Tech (CSE)
File I/O - Operations
File Open statement
In Python, file operations take place in the following order:
• Open the file
• Read / Write (perform operation on the file)
• Close the file

Create and Open a file in Python:


• Before you can perform any operations on a file, like reading, writing, or printing its
contents, you must first open the file.
• To open a file, we must use the built-in function open() which is most commonly used
with two arguments namely filename, and mode

• The open() function returns a file object, also known as a file handle, which allows
you to perform various operations on the file.

Faculty Name: Pragya Agarwal Programe Name: B.Tech (CSE)


File Open statement

Syntax:
file_object = open(filename, mode)
file name - It is the first argument and is a string representing the name of
the file or the location of the file which we want to access, and it should be
included along with its extension.
mode - It is the second argument that specifies the type of operation to be
performed on a file. It is optional and defaults to read-only ('r') mode, which
allows us to retrieve text data from the file after reading it.
Example:
f = open('sample1.txt', 'w')
It will try to open a new file with the name sample1.txt in write mode in the
current directory.
Faculty Name: Pragya Agarwal Programe Name: B.Tech (CSE)
Exercise

Select all the correct statements from the given options:

 An open() statement returns the contents of the file


 The mode argument tells how the file is to be opened, e.g. for reading
only, for writing only etc.
 The mode argument is a mandatory argument
 If we do not specify any path, the file is assumed to be in the current
directory

Faculty Name: Pragya Agarwal Programe Name: B.Tech


Mode of File

The mode in the syntax of open() function will tell the type of operation to be performed on
a file.
The following modes are for opening, reading or writing text files only:
'r' – Read mode is used only to read data from the file.
‘w'– Write mode is used to write data into a file or modify it and will create a new file if the
same does not exist. Remember that this mode can overwrite the data present in the file.
‘a' – Append mode is used to append data to a file. Remember that the data will be
appended at the end of the file pointer. If the file doesn’t exist, then a new file will be
created.
'x' – It is used to open a file for exclusive creation. If the file already exists, the operation will
fail.
‘r+' – Read or Write Mode, used to read and write the data from and to the same file.
‘a+' – Append or Read Mode, used to read data from the file or append the data to the end
of the same file.
Faculty Name: Pragya Agarwal Programe Name: B.Tech
Reading File in Python

After we open a file, we use In the above example, we have


the read() method to read its contents. read the test.txt file that is available
For example: in our current directory.
# open a file
file1 = open("test.txt", "r") Notice the code,
# read the file read_content = file1.read( )
read_content = file1.read() Here, file1.read() reads
print(read_content) the test.txt file and is stored in
the read_content variable.
Output:
This is a test file.
Hello from the test file.
Faculty Name: Pragya Agarwal Programme Name: B.Tech
Closing File in Python

Closing a file will free up the resources Output


that were tied with the file. It is done This is a test file.
using the close() method in Python. Hello from the test file.

For example: Here, we have used


# open a file the close() method to close the file.
file1 = open("test.txt", "r")
# read the file After we perform file operation, we
read_content = file1.read() should always close the file; it's a
print(read_content) good programming practice.
# close the file
file1.close()
Faculty Name: Pragya Agarwal Programme Name: B.Tech
Writing to Files in Python

There are two things we need to remember while writing to a file.


• If we try to open a file that doesn't exist, a new file is created.
• If a file already exists, its content is erased, and new content is added to
the file.
In order to write into a file in Python, we need to open it in write mode by
passing "w" inside open() as a second argument.
Suppose, we don't have a file named test2.txt. Let's see what happens if we
write contents to the test2.txt file.
file2 = open(test2.txt', 'w')
# write contents to the test2.txt file
file2.write('Programming is Fun.')
file2.write('Programiz for beginners')
Faculty Name: Pragya Agarwal Programe Name: B.Tech
Deleting and Renaming Files in Python

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. os.rmdir() can be used to remove directory.
Syntax: os.remove(path, *, dir_fd = None)

To rename a file or directory in Python you can use os.rename() function of OS module.
This method renames a source file or directory to a specified destination file or directory
Import os
os.rename(source, destination, *, src_dir_fd = None, dst_dir_fd = None)

Faculty Name: Pragya Agarwal Programe Name: B.Tech


Working with Binary Files in Python

https://www.geeksforgeeks.org/reading-binary-files-in-python/

https://www.w3schools.com/python/python_file_handling.asp

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Using Pickle to write in Binary Files

• When we want to store dictionaries, tuples, lists, class objects or any other data type
to the disk and use them later, we need to convert these objects in the form of bytes,
to store them in Binary Files. This process is called Pickling or Serialization. It is
done using the dump() method of ‘pickle’ module.

pickle.dump (object , file)

• Pickle can be used to serialize Python object structures, which refers to the process of
converting an object in the memory to a byte stream that can be stored as a binary file
on disk. When we load it back to a Python program, this binary file can be de-
serialized back to a Python object. This process is called Unpickling or
Deserialization. It is done using load() Method of pickle module.
• object=pickle.load (file)

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Regular Expression

A Regular Expression or RegEx is a special sequence of characters that


uses a search pattern to find a string or set of strings.

It can detect the presence or absence of a text by matching it with a particular


pattern and also can split a pattern into one or more sub-patterns.

We have to import re module to use Regular Expression.

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Regular Expression

import re

s = ‘Python: An all purpose Programming Language '

obj = re.search(’Language', s)

print('Start Index:’, obj.start())


print('End Index:’, obj.end())

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Regular Expression
The re module offers following set of functions that allows us to
search a string for a match: -

1.findall() - Returns a list containing all matches.


2.search() - Returns a Match object if there is a match anywhere
in the string.
3.split() - Returns a list where the string has been split at each
match
4. sub() - Replaces one or many matches with a string.

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Regular Expression

The findall() function returns a list containing all matches. The


list contains the matches in the order they are found.
If no matches are found, an empty list is returned:

import re

txt = "The rain in Spain"


x = re.findall("ai", txt)
print(x)

Oputput:-
['ai', 'ai']

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Regular Expression
The search() function searches the string for a match, and returns a Match
object if there is a match.

If there is more than one match, only the first occurrence of the match will be
returned.
If there is no match, the value None will be returned, instead of the Match
Object.
Example: -
Search for the first white-space character in the string.

import re

txt = "The rain in Spain"


x = re.search("\s", txt)

print("The first white-space character is located in position:",


x.start())

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Regular Expression

A Match Object is an object containing information about the


search and the result.

The Match object has properties and methods used to retrieve


information about the search, and the result:
.span() returns a tuple containing the start-, and end positions of
the match.
.string returns the string passed into the function
.group() returns the part of the string where there was a match

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Regular Expression

Example: -
Print the position (start- and end-position) of the first match occurrence.
Print the string passed into the function.
Print the part of the string where there was a match.
The regular expression looks for any words that starts with an upper case "S“.

import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
print(x.string)

Output: -

(12, 17)
The rain in Spain
Spain

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Regular Expression

The split() function returns a list where the string has been split at each match.

Example: -

Split at each white-space character:

import re

txt = "The rain in Spain"


x = re.split("\s", txt)
print(x)

Output: -
['The', 'rain', 'in', 'Spain']

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Regular Expression

The sub() function replaces the matches with the text of your choice.

import re
txt = "The rain in Spain"
x = re.sub("\s", "9", txt)
print(x)

Output: -
The9rain9in9Spain

Refer the following link to study all the methods in the Regular Expression.
https://www.w3schools.com/python/python_regex.asp

For wildcard searching: -


https://www.geeksforgeeks.org/python-wildcard-substring-search/

Faculty Name: Pragya Agarwal Programme Name: B.Tech


Wild Card Search

Sometimes, while working with Python Strings, we have problem in which, we need to
search for substring, but have some of characters missing and we need to find the match.
This can have application in many domains.

Faculty Name: Pragya Agarwal Programme Name: B.Tech

You might also like