Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python File Handling (1)

The document provides an overview of Python file handling, detailing the importance of files for permanent data storage and the basic operations involved, such as opening, reading, writing, and closing files. It explains various access modes, including read, write, append, and their binary counterparts, along with methods for reading file contents and manipulating file pointers. Additionally, it includes examples of file operations and a sample program for counting lines in a file.

Uploaded by

Gautham J K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python File Handling (1)

The document provides an overview of Python file handling, detailing the importance of files for permanent data storage and the basic operations involved, such as opening, reading, writing, and closing files. It explains various access modes, including read, write, append, and their binary counterparts, along with methods for reading file contents and manipulating file pointers. Additionally, it includes examples of file operations and a sample program for counting lines in a file.

Uploaded by

Gautham J K
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Module -IV

Python File Handling


Python File Handling
• The file handling plays an important role when the data needs to be
stored permanently into the file.

• A file is a named location on disk to store related information.

• can access the stored information (non-volatile) after the program


termination.
Python File Handling
Python File Handling
A file operation can be done in the following order
• Open a file
• Read or write - Performing operation
• Close the file
Opening a file
• Python provides an open() function that accepts two arguments
• file name and access mode in which the file is accessed

file object = open(<file-name>, <access-mode>)

• The function returns a file object which can be used to perform


various operations like reading, writing, etc.
Python File Handling: Basic Access Modes

1. Read Only ( r ): open an existing file for a read operation.

2. Write Only (w): open an existing file for a write operation.


• if the file already contains data will be overridden
• if the file is not present then it creates the file as well

3. Append (a): open an existing file for append operation


It won’t override existing data.
File Open
Read(), readline() and readlines()
• Python allows you to read the contents of a file using methods such
as:
1.read()
2.readline()
3.readlines()
Read(), readline() and readlines()
read()
• The read method reads the entire contents of a file and returns it as a string.
readline()
• reads a single line from a file and returns it as a string.
• read the contents of a file line by line, which can be useful for processing
large files that do not fit in memory.
readlines ( )
• Reads all the lines and returns them as a string element in a list.
• Readlines ( ) is used to read all the lines at a single go and then return them as
a string element in a list.
Read()
• The read() method returns the specified number of bytes from the
file.
file_name.read(<size>)
Readline()
The readline() method returns one line from the file.

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.

2. Write and Read (w+): To write and read data.


It will override existing data.

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

You might also like