File Handing For Students
File Handing For Students
What is file:
A file is a sequence of character/bytes on the disk/permanent storage where a group
of related data is stored. File is created for permanent storage of data.
What is use of files:
File is created for permanent storage of data.
Advantanges of file:
Data stored permanently.
Stored data can be shared.
We can modify data later on.
What is file handling:
File handling in Python enables us to create, update, read, and delete the files stored
on the file system through our python program. The following operations can be
performed on a file:
In Python, File Handling consists of following three steps:
Open the file.
Performing operation (Read, Write, Append, Insert, Delete, etc.) or Processing
data.
Close the file.
Type of file:
1) Text File:
A file whose contents can be viewed using a text editor is called a text file.
A text file is simply a sequence of ASCII or Unicode characters.
Default character coding in python is ASCII, but using constant ‘u’ with string, it
support Unicode also.
In text file, each line is terminated by a special character known as End of Line
(EOL) also called (‘\n’). Example: .txt, .csv, .rtf .
2) Binary file:
A binary file stores the data in the same way as stored in the memory.
We can’t read a binary file using a text editor.
There is no delimiter for a line.
Example: .exe files,mp3 file, image files, word documents.
3) CSV file:
A CSV stands for Comma Separated Values.
CSV format is one of the most simple and common ways to store tabular data.
To represent a CSV file, it must be saved with the .csv file extension.
CSV is just like text file, contents can be viewed using a text editor is called a
text file.
The default delimiter or separator is (,) comma. Other delimiters are tab(‘\t’),
Colon(:),pipe(|), and semicolon (;).
Write():
This method takes string type data. It is used to write single line in text file.
If file doesn’t exist, it will create the new file. And if file is exist it overwrite the
data.
For storing data in new line we will have to add ‘\n’ (2 bytes) end of line
character.
Syntax: handle.write(string)
For storing numeric data value in text file, conversion to string is required.
Writelines(): This function is used to write multiple data in form of sequence data
type(string, list and tuple).
Syntax: handle.readline()
Tell(): This function is used to return current position of file pointer, Example: p.tell().
*Use of r+, w+, a+
Manipulation of data in a text file:
This concept is fully based on overwriting of data.
Example:
p=open('demo2.txt','r')
k=p.read()
data=k.swapcase()
p.close()
p=open('demo2.txt','w')
k=p.write(data)
p.close()
Second Way
p=open('demo5.txt','r+')
k=p.read()
data=k.swapcase()
p.seek(0)
k=p.write(data)
p.close()
Methods of OS module:
1. The rename() method used to rename the file.
Syntax os.rename(current_file_name, new_file_name)
2. The remove() method to delete file.
Syntax os.remove(file_name)
3.The mkdir() method of the os module to create directories in the current directory.
Syntax os.mkdir("newdir")
4.The chdir() method to change the current directory.
Syntax os.chdir("newdir")
5.The getcwd() method displays the current directory.
Syntax os.getcwd()
6. The rmdir() method deletes the directory.
Syntax os.rmdir('dirname')
import pickle
import os
def write():
file=open('demo.txt','ab')
y='y'
while y=='y':
roll=int(input('Enter roll no.:'))
name=input('Enter name:')
marks=int(input('Enter Marks:'))
data=[roll,name,marks]
pickle.dump(data,file)
y=input('Do you want to enter data again:')
file.close()
def read():
file=open('demo.txt','rb')
try:
while True:
i=pickle.load(file)
print(i)
except EOFError:
print()
file.close()
file=open('demo.txt','rb')
file2=open('demo2.txt','wb')
roll=int(input('Enter roll no.:'))
try:
while True:
i=pickle.load(file)
if i[0]!=roll:
pickle.dump(i,file2)
except EOFError:
print("")
file.close()
file2.close()
os.remove('demo.txt')
os.rename('demo2.txt','demo.txt')