Professional Documents
Culture Documents
File
Files are named locations on disk to store related information. They are used to permanently store
data in a non-volatile memory (e.g. hard disk).
or
A file is a stream or sequence of characters/bytes (sequence of related data) occupying a named
place on the disk.
INTRODUCTION
FILE HANDLING is a mechanism by which we can read data of disk files in python
program or write back data from python program to disk files.
So far in our python program the standard input is coming from keyboard an output is
going to monitor i.e. nowhere data is stored permanently and entered data is present as long
as program is running. BUT file handling allows us to store data entered through python
program permanently in disk file and later on we can read back the data.
** EOL: End of Line
read(): returns a string read from the file.
read(n): If n is not specified or negative value for n is specified as size, then the entire file will
be read.
readline(): For readline(), a line is terminated by ‘\n’.
////
** For storing data with end of line character, we will have to add ‘\n’ character to the end
of the string, and it is of 2 bytes.
If we do not provide \n the entire data will be written to a single line.
** If file does not exists in the current directory, a new file is created.
** If file exists, data present in the file shall be overwritten every time we run the program.
* for storing numeric data value in a text file, conversion to string is required.
Example:
>>>x=52
>>>file1.write (str(52))
** writelines() accepts any type of sequence as an argument.
** In writelines() also we have to add \n explicitly.
Random Access in files
Python allows random access of the file data using built-in functions:
1. tell()
2. seek()
tell() function : is used to get the actual position of file object/handle. By file object/handle we
mean a cursor. And it’s cursor, who decides from where data has to be read or written in a file.
Syntax:
f.tell(), where f is the file handle/object.
Returns: returns the position of file object/handle.
seek(): It is used to shift/change the position of file object to required position. By file object we
mean a cursor. And it’s cursor, who decides from where data has to be read or write in a file.
Syntax:
f.seek(offset, from_what), where f is file pointer.
Parameters:
Offset: Number of positions to move forward
from_what: It defines point of reference.
The reference point is selected by the from_what argument. It accepts three values:
test.txt
Program:
Output:
File Opening modes:
Difference between r and r+ in open()
The below example uses f.read() to move the file pointer to the end of the file, and append a new
line.
Difference between w and w+ in open()
If the file does not exist, r+ throws FileNotFoundError; the w+ creates the file.
If the file exists, r+ opens it without truncating; the w+ truncates the file and opens it.
Difference between r+ and a+ in open()
If the file does not exist, r+ throws FileNotFoundError; the a+ creates the file.
For r+ mode, the initial file pointer position at the beginning of the file; For a+ mode, the
initial file pointer position at the end of the file.
Difference between w+ and a+ in open()
If the file exists, w+ truncates the file and opens it; a+ opens it without truncating.
For w+ mode, the initial file pointer position at the beginning of the file; For a+ mode, the
initial file pointer position at the end of the file.
A path to an entity (in this case, a file, folder, or web page) describes the entity’s unique location
within a hierarchical directory or website structure.
The absolute path (also known as the full path) of an entity contains the complete information
(from the root to the ending) needed to locate it. The absolute path is not affected by the user’s
current working directory, and it always includes the root directory as a starting point.
Note: Here, C:\ is the root directory, and C:\Recipes is the current working directory.
Absolute paths are easier to understand, but they can be inconvenient to work with as each step
from the root to the entity needs to be included.
Relative Path
The relative path of an entity contains the information needed to locate that entity relative to the
user’s current working directory. The relative path disregards the information needed to locate the
current working directory from the root directory and only focuses on the route from the working
directory to the entity.
Since the relative path uses the current working directory as a vantage point, the Macaroni file’s
relative path is Pasta\Macaroni and the relative path to the CheeseCake file is Cakes\CheeseCake.
Note how we are simply studying the file paths relative to the current working
directory, C:\Recipes.
Although relative paths hold less information than absolute paths, they are shorter and easier to
work with (especially in deeply nested directories).
Test.txt
We are students of class 12 D.
We all are together.
We all are good.
Output:
Prog 3: Menu-Driven Program
Output:
Prog 1: Write a program to write the below 2 rows to a binary file “Student.dat”.
[23,’John’,12]
[25,’Mira’,13]
Prog 2:
Write a program to write Name and Roll No into a binary file “file.dat”. The data should be in
the given format:
(Name, Roll No)
Prog 3:
Write a program to write [code,name,amount] into a binary file “customer.dat”, where the data is
taken from the user.
Prog 4:
Write a program to read all the contents from the binary file “file.dat”. The file has data as a list
[name,rollno]
Prog: Update the salary of customer in “demo.dat” file whose name is “Anu”.
Prog: Update the salary of customer in “demo.dat” file where city is “Delhi”.
import pickle
f=open('customer.dat','wb')
pickle.dump(['C101','Nidhi','Delhi',2000],f)
pickle.dump(['C102','Manisha','Pune',3000],f)
f.close()
Prog:
Select and print all rows where city is “salary” is greater than 2000 from “customer.dat” file.
Data is stored in this format:[City,Cust Name, Amount]
Output:
Using the same template you can apply various selection crieteria:
1. Select and print sum of salaries where city is “Delhi” from “demo.dat” file.
Data is stored in this format:[City,Cust Name, Amount]
Output:
Output:
Prog : Program to copy one file into another file.
Prog 1:
Write a program to display only student name from binary file “file.dat”. The file has data as list
(name,rollno).
Output:
Prog 2:
Select and print all rows where city is “Delhi” from “customer.dat” file.
Data is stored in this format:[City,Cust Name, Amount]
Output:
* For working with CSV files in Python, there is an in-built module called csv. It is used to
read and write tabular data in CSV format.
r- raw data
Output
# Program to search the record of a particular student from csv file on the basis of inputted
name.
import csv
f=open('Student.csv','r')
csv_reader=csv.reader(f)
name=input('Enter the name that you want to search')
for row in csv_reader:
if row[1]==name:
print(row)
f.close()
Python program to print records in the form of comma seperated values.
# Program to write one record at a time in csv file using writerow function
import csv
f=open('Demo.csv','w')
csv_w=csv.writer(f)
csv_w.writerow(['Name','Age'])
csv_w.writerow(['Anu',16])
csv_w.writerow(['Tanu',17])
f.close()
Output:
** To suppress EOL character, use newline=’’ while opening file.
import csv
f=open('Demo.csv','w',newline='')
csv_w=csv.writer(f)
csv_w.writerow(['Name','Age'])
csv_w.writerow(['Anu',16])
csv_w.writerow(['Tanu',17])
f.close()
# use of writerows()
import csv
f=open('Demo.csv','w',newline='')
values=[['Name','Age'],['Anu',16],['Tanu',17]]
csv_w=csv.writer(f)
csv_w.writerows(values)
f.close()