File Handling
File Handling
File Handling
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
• The open() function returns a file object, also known as a file handle, which allows
you to perform various operations on the file.
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
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
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)
https://www.geeksforgeeks.org/reading-binary-files-in-python/
https://www.w3schools.com/python/python_file_handling.asp
• 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 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)
import re
obj = re.search(’Language', s)
import re
Oputput:-
['ai', 'ai']
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
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
The split() function returns a list where the string has been split at each match.
Example: -
import re
Output: -
['The', 'rain', 'in', 'Spain']
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
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.