Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
1. Name of the Teacher: Dr. Madhavi Vaidya Class:FYBSC Subject: Computer Sc Paper: Python II
Objectives of File Handling
• File
• Working with open()
• read()
• open()
• write()
• readline()
• Close()
2. File
• Python too supports file handling(like other programming languages) and
allows users to handle files i.e., to read and write files, along with many other
file handling options, to operate on files.
• The concept of file handling has stretched over various other languages, but
the implementation is either complicated or lengthy, but alike other concepts
of Python, this concept here is also easy and short.
• Python treats file differently as text or binary and this is important.
• Each line of code includes a sequence of characters and they form text file
Hence, a file operation can be done in the following order.
•Open a file
•Read or write - Performing operation
•Close the file
3. Working with open()
We use open () function in Python to open a file in read or write mode. As explained
above, open ( ) will return a file object.
To return a file object we use open() function along with two arguments, that accepts
file name and the mode, whether to read or write.
So, the syntax being: open(filename, mode).
There are three kinds of mode, that Python provides and how files can be opened:
•“ r “, for reading.
•“ w “, for writing.
•“ a “, for appending.
•“ r+ “, for both reading and writing
•“x" - Create - Creates the specified file, returns an error if the file exists
•"t" - Text - Default value. Text mode
•"b" - Binary - Binary mode (e.g. images)
• Refer file3new.py
4. Open()
• 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.
• The open command will open the file in the read
mode and the for loop will print each line present in
the file.
• Refer file3new.py
5. “rt” mode/no mode/”r” mode
• To open a file for reading it is enough to specify the name of the file:
• f = open("demofile.txt") [by x/w parameter we can create a new file too if not
present]
• The code above is the same as below:
• f = open("demofile.txt", "rt")
• f = open("demofile.txt", "r")
6. read()
• Reading the contents of the file
• The open() function returns a file object, which has a read() method for reading the
content of the file:-
f = open("demofile.txt", "r")
print(f.read())
• Read Only Parts of the File(in bytes)
By default the read() method returns the whole text, but you can also specify how
many characters you want to return:
Read() can be used with/without parameters
f = open("demofile.txt", "r")
print(f.read(5))
-------------------------------------------
Next slide….read(),readline(),readlines()
Use file1.py – from PD/file
7. read(),readline(),readlines()
f=open("foo.txt","r")
print(f.read()) #all lines would be read
#print(f.read(4)) #it would read 4 bytes
#print(f.readline(3)) #would read char by char ahead
print(f.readlines()) # all lines would be read with n delimiter
1.Read ( ): Returns the read bytes in the form of a string. ...
2.Readline ( ): Reads a line of the file and returns in the form of a
string (if parameter passed then that many bytes, else first line
3.readlines ( ): Reads all the lines and returns them as a string
element in a list with a new line char after every line
8. Creating a file using write() mode
• Using write() mode
To manipulate the file, write the following in
your Python environment:
# Python code to create a file
file = open('geek.txt','w')
file.write("This is the write command")
file.write("It allows us to write in a particular
file")
file.close()
• Refer file7.py
• Working of append() mode
Python code to illustrate append mode
file = open('geek.txt’,'a')
file.write("This will add this line")
file.close()
file = open("foo.txt","w")
file.write("This is the write command")
file.write("It allows us to write in a particular file")
file = open("foo.txt","r") #if we dont write this, the file can’t be read
print(file.read())
file.close()
------------------------------
#Python program to append contents of one file to another file
name1=input("enter the file to be read from:")
name2=input("enter the file to be appended into")
in1=open(name1,"r")
data=in1.read()
in1.close()
out=open(name2,"a")
out.write(data)
out.close()
9. Read Lines
• You can return one line by using the readline() method:
• Read one line of the file:
f = open("demofile.txt", "r")
print(f.readline())
By calling readline() two times, you can read the two first lines:
Example
Read two lines of the file:
f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
10. Looping thru the file and close()
• By looping through the lines of the file, you can read
the whole file, line by line:
f = open("demofile.txt", "r")
for x in f:
print(x)
• Closing the file –close()
f = open("demofile.txt", "r")
print(f.readline())
f.close()
11. Writing to an Existing file
To write to an existing file, you must add a parameter to the open()
function:
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
12. Writing to the file by “w” mode
# open the file.txt in append mode. Create a new file if no such file exists.
fileptr = open("file2.txt", "w")
# appending the content to the file
fileptr.write('''''Python is the modern day language. It makes things so simp
le,It is the fastest-growing programing language''')
# closing the opened the file
fileptr.close()