Professional Documents
Culture Documents
Unit VI File Handling and Dictionaries (07 HRS)
Unit VI File Handling and Dictionaries (07 HRS)
Unit VI File Handling and Dictionaries (07 HRS)
File path, types of files, opening –closing files, reading and writing files,
Dictionary method.
f = open(filename, mode)
3. a: open an existing file for append operation. It won’t override existing data.
4. r+: To read and write data into the file. The previous data in the file will be overridden.
5. w+: To write and read data. It will override existing data.
6. a+: To append and read data from the file. It won’t override existing data.
Creating a file using write() mode
file = open('pps.txt','w')
file.write("Hello")
file.write("Welcome")
file.close()
There is more than one way to read a file in Python. If you need to extract a string
that contains all characters in the file then we can use file.read().
file.close()
Another way to read a file is to call a certain number of characters like in the
following code. The interpreter will read the first two characters of stored data and
return it as a string:
print(file.read(2))
file.close()
Working of append() mode
file = open('pps.txt','a')
file.close()
One must keep in mind that the mode argument is not mandatory. If not
passed, then Python will assume it to be “ r ” by default.
PPS UNIT-6 FILE HANDLING AND DICTIONARY (Questions and Answers)
1. Text File
● Text file store the data in the form of characters.
● Text file are used to store characters or strings.
● Usually we can use text files to store character data
● eg: abc.txt
2. Binary File
● Binary file store entire data in the form of bytes.
● Binary file can be used to store text, image, audio and video.
● Usually we can use binary files to store binary data like images,video files, audio files
etc
Q.What is Directory? Discuss some directory method present in os module. Give
Example. Answer :
-Directory is collection similar or different types of files. os module provides various methods
to create, remove and change directory.
2. getcwd( ) method- This method display the current working directory (cwd).
Syntax for getcwd() method-
os.getcwd()
Example Program to display current working directory
import os
print(os.getcwd()) #home
3. chdir( ) method-
It is used to change the current directory.
Syntax for chdir() method-
os.chdir('directoryname')
4. rmdir( ) method-
it is used to remove or delete directory.
Syntax for rmdir() method-
os.rmdir('directoryname')
listdir() method in python is used to get the list of all files and directories in
the specified directory.
import os
print(os.listdir())
Q.Differentiate between absolute path and relative path.
Q.Explain rename() and remove() method of os module.
Os module in python is used to perform file processing operations like rename and delete file. To
use these methods from os module, first import os module in python program.
rename() method
It takes two arguments current filename and new file name
syntax to rename- os.rename(oldfilename,newfilename)
Example to rename file
import os
os.rename('file1.txt','file2.txt')
print('file rename successfully')
remove() method
it is used to delete file.
It takes one argument as file name to delete.
Syntax to delete file-
os.remove(filename)
Example to delete file
import os
os.remove('file1.txt')
print('file deleted successfully')
One must keep in mind that the mode argument is not mandatory. If not passed,
then Python will assume it to be “ r ” by default. Let’s look at this program and try
to analyze how the read mode works:
print (each)
The open command will open the file in the read mode and the for loop will
print each line present in the file.
Thanks