Python File Handling (1)
Python File Handling (1)
You can also specified how many bytes from the line to return, by using
the size parameter.
file.readline(<size>)
Read Lines
Readlines()
• returns a list containing each line in the file as a list item.
file.readlines(hint)
seek()
• The seek() method sets the current file position in a file stream.
• The seek() method also returns the new position.
file.seek(offset)
tell()
• The tell() method returns the current file position in a file stream.
file.tell()
Write to an Existing File
Write to an Existing File
• Open the file "demofile3.txt" and overwrite the content:
Using write along with the with() function
Using write along with the with() function
Python File Handling: Other Access Modes
1. Read and Write (r+): To read and write data into the file.
The previous data in the file will be overridden.
3. Append and Read (a+): To append and read data from the file. It
won’t override existing data.
Read and write a file with r+
• In r+ mode, we can read and write the file, but the file pointer
position is at the beginning of the file; if we write the file directly, it
will write in the beginning.
Read and write a file with r+
f.read() move the file pointer to the end of the file, and
append a new line.
Python File Handling: Access Modes
4. Write Only in Binary Format(‘wb’): It lets the user open the file for writing in binary
format. When a file gets opened in this mode, there are two things that can happen
mostly. A new file gets created if the file does not exist. The content within the file will get
overwritten if the file exists and has some data stored in it.
5. Write and Read in Binary Format(‘wb+’): It lets the user open the file for reading as
well as writing in binary format. When a file gets opened in this mode, there are two things
that can mostly happen. A new file gets created for writing and reading if the file does not
exist. The content within the file will get overwritten if the file exists and has some data
stored in it.
6. Append only in Binary Format(‘ab’): It lets the user open the file for appending in binary
format. A new file gets created if there is no file. The data will be inserted at the end if the
file exists and has some data stored in it.
7. Append and Read in Binary Format(‘ab+’): It lets the user open the file for appending
and reading in binary format. A new file will be created for reading and appending if the
file does not exist. We can read and append if the file exists and has some data stored in it.
Python File Handling: Access Modes
8. Read Only in Binary format(‘rb’): It lets the user open the file for
reading in binary format.
9. Read and Write in Binary Format(‘rb+’): It lets the user open the file
for reading and writing in binary format.
Difference between r+ and w+ in open()
Difference between w+ and a+ in open()
• For w+ mode, the initial file pointer position at the beginning of the
file; For a+ mode, the initial file pointer position at the end of the file.
Python Delete File
• To delete a file, you must import the OS module, and run its
os.remove() function:
Check if File exist
Delete Folder
Write a program to count the total number of lines and count the total
number of lines starting with ‘A’, ‘B’, and ‘C’. (Consider the merge.txt file)
Write a program to count a total number of lines and count the total
number of lines starting with ‘A’, ‘B’, and ‘C’. (Consider the merge.txt file)
def program5():
with open("merge.txt","r") as f1: print("Total Number of lines are:",cnt_lines)
data=f1.readlines() print("Total Number of lines strating with A
cnt_lines=0 are:",cnt_A)
cnt_A=0 print("Total Number of lines strating with B
cnt_B=0 are:",cnt_B)
cnt_C=0 print("Total Number of lines strating with C
for lines in data: are:",cnt_C)
cnt_lines+=1
if lines[0]=='A':
cnt_A+=1
if lines[0]=='B': program5()
cnt_B+=1
if lines[0]=='C':
cnt_C+=1
Write a Python program to combine each line from first
file with the corresponding line in second file