0% found this document useful (0 votes)
97 views

File handling (1)

.

Uploaded by

sakshamdubey022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

File handling (1)

.

Uploaded by

sakshamdubey022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

File handling in Python-

A file is a named location used for storing data. For example, main.py is a file that is always used to store
Python code.
Python provides various functions to perform different file operations, a process known as File Handling.

File handling modes –

1. Read ('r'): Opens a file for reading. If the file does not exist, it raises a FileNotFoundError.

2. Write ('w'): Opens a file for writing. If the file exists, it truncates the file (erases its content). If the
file does not exist, it creates a new one.

3. Append ('a'): Opens a file for appending. It does not truncate the file; new data is written at the end.
If the file does not exist, it creates a new one.

4. Read and Write ('r+'): Opens a file for both reading and writing. The file must exist.

5. Write and Read ('w+'): Opens a file for both writing and reading. It truncates the file if it exists; if
not, it creates a new one.

6. Append and Read ('a+'): Opens a file for both appending and reading. It creates a new file if it
does not exist.

7. Binary Modes: You can also open files in binary mode by adding a 'b' to the mode string. For
example:
• Read binary ('rb')
• Write binary ('wb')
• Append binary ('ab')
• Read and write binary ('r+b')
• Write and read binary ('w+b')
• Append and read binary ('a+b')

Opening Files in Python


In Python, we need to open a file first to perform any operations on it—we use the open() function to do so.
Let's look at an example:
Suppose we have a file named file1.txt.
To open this file, we can use the open() function.
file1 = open("file1.txt")
Here, we have created a file object named file1. Now, we can use this object to work with files.

Reading Files in Python-


After we open a file, we use the read() method to read its content. For example,
Suppose we have a file named file1.txt.

Now, let's read the content of the file.


# open a file in read mode
file1 = open("file1.txt")

# read the file content


read_content = file1.read()
print(read_content)
Output
This is a test file.
Hello from the test file.

In the above example, the code file1.read() reads the content of the file and stores it in
the read_content variable.

Writing to Files in Python-


To write to a Python file, we need to open it in write mode using the w parameter.
Suppose we have a file named file2.txt. Let's write to this file.
# open the file2.txt in write mode
file2 = open('file2.txt', 'w')
# write contents to the file2.txt file
file2.write('Programming is Fun.\n')
file2.write('Programiz for beginners\n')
When we run the above code, we will see the specified content inside the file.

Closing Files in Python


When we are done performing operations on the file, we need to close the file properly. We use
the close() function to close a file in Python. For example,
# open a file
file1 = open("file1.txt", "r")

# read the file


read_content = file1.read()
print(read_content)

# close the file


file1.close()
Output
This is a test file.
Hello from the test file.
Note: Closing a file will free up the resources that are tied to the file. Hence, it is a good programming
practice to always close the file.

Append to files in Python-


To append data to a file in Python, you can use the 'a' mode when opening the file. This will allow you to add
new content to the end of the file without removing the existing content. Here’s an example:

# Appending text to a file


file1= open('file1.txt', 'a')
file1.write('This is a new line added to the file.\n')
# Verifying the content of the file
file1=open('file1.txt', 'r')
read_content = file1.read()
print(read_content)
file1.close()
Output-
This is a test file.
Hello from the test file.
'This is a new line added to the file

Readline ()and ReadLines () in Python-

Using readline () Method-


The readline() method reads a single line from a file each time it is called. This can be useful when you want
to process a file line by line without loading the entire file into memory.

Using readlines () Method-


The readlines() method reads all the lines of a file and returns them as a list of strings. This method is useful
when you want to read all lines at once and process them as a list.
Differences Between readline() and readlines()
The key differences between readline() and readlines() are:
▪ readline(): Reads one line at a time. Useful for large files to avoid memory issues.
▪ readlines(): Reads all lines and returns them as a list. Convenient for smaller files or when you need
all lines at once.
Choosing the right method depends on the file size and how you intend to process the file content.

You might also like