You are on page 1of 7

Python Programming Lab

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.

Practical No. 09

Name of Student: Himanshu Shrivastav Year/Sem. : II/III

Roll No.: AM21048 Academic Session: 2022-2023


Date of Performance: Date of Submission:

Department of Emerging Technologies (AIDS& AIML), S.B.J.I.T.M.R, Nagpur.


1
Python Programming Lab

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

Department of Emerging Technologies (AIDS& AIML), S.B.J.I.T.M.R, Nagpur.


2
Python Programming Lab

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

Print the content


of file.

end

Department of Emerging Technologies (AIDS& AIML), S.B.J.I.T.M.R, Nagpur.


3
Python Programming Lab

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

Department of Emerging Technologies (AIDS& AIML), S.B.J.I.T.M.R, Nagpur.


4
Python Programming Lab

Destination file

Destination file after copy the contents of source file

Department of Emerging Technologies (AIDS& AIML), S.B.J.I.T.M.R, Nagpur.


5
Python Programming Lab

CONCLUSION: We have learnt and understand the concept of Files in Python. We have also
learnt various operations that can be performed on Files.

Department of Emerging Technologies (AIDS& AIML), S.B.J.I.T.M.R, Nagpur.


6
Python Programming Lab

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:

1. “Learning Python”, Mark Lutz, 4thEdition, 2000, O'Reilly Media, Inc.

2. “Python: The complete Reference”, Martin C Brown, 1stEdition, 2001, McGraw Hill.

Department of Emerging Technologies (AIDS& AIML), S.B.J.I.T.M.R, Nagpur.


7

You might also like