Professional Documents
Culture Documents
Practical No. 09
Aim: Develop an application using file handling to copy the contents of python
script into another without including the comments. Ask the user about the source
and destination file names. Print the content of the both the files.
OBJECTIVE/EXPECTED LEARNINGOUTCOME:
The objectives and expected learning outcome of this practical are:
• To understand the concept of Files and its implementation in Python.
• To understand various operations on Files for analyzing and representing the text in Python.
THEORY:
Files in Python:
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).
Since Random Access Memory (RAM) is volatile (which loses its data when the computer is turned
off), we use files for future use of the data by permanently storing them.
When we want to read from or write to a file, we need to open it first. When we are done, it needs
to be closed so that the resources that are tied with the file are freed.
Hence, in Python, a file operation takes place in the following order:
1. Open a file
2. Read, write or append (perform operation)
3. Close the file
Python too supports file handling and allows users to handle files i.e., to read and write files, along
with many other file handling options, to operate on files. Python treats files differently as text or
binary and this is important. Each line of code includes a sequence of characters and they form a
text file. Each line of a file is terminated with a special character, called the EOL or End of Line
characters like comma {,} or newline character. It ends the current line and tells the interpreter a
new one has begun.
FLOWCHART:
START
Import shutil
Shutil.copy
source file to dest file
end
PROGRAM:
import shutil
x=shutil.copy('source.txt','dest.txt')
f = open("source.txt", "r")
print(f.read())
r= open("dest.txt","r")
print(r.read())
OUTPUT:
Source file
Destination file
CONCLUSION: We have learnt and understand the concept of Files in Python. We have also
learnt various operations that can be performed on Files.
DISCUSSION QUESTIONS:
1) List out the different access modes and operations that can be performed on files.
2) Identify 5 applications where we require Files in problem solving.
REFERENCE:
2. “Python: The complete Reference”, Martin C Brown, 1stEdition, 2001, McGraw Hill.