13538_253_125_Python__file_handling_M3
13538_253_125_Python__file_handling_M3
Output:
HI GOOD MORNING STUDENTS
Example 3: In this example, we will see how we can read a file
using the with statement.
Python3
print(data)
Output:
Hello
Example 5:
We can also split lines while reading files in Python. The split()
function splits the variable when space is encountered. You can also
split using any characters as you wish.
Python3
Output:
['Hi', 'good']
['morning']
['students']
Creating a File using the write() Function
Just like reading a file in Python, there are a number of ways to write
in a file in Python. Let us see how we can write the content of a file
using the write() function in Python.
Working in Write Mode
Let’s see how to create a file and how the write mode works.
Example 1: In this example, we will see how the write mode and
the write() function is used to write in a file. The close() command
terminates all the resources in use and frees the system of this
particular program.
Python3
Output:
This is the write commandIt allows us to write in a particular
file
Example 2: We can also use the written statement along with the
with() function.
Python3
Output:
Hello World!!!
Working of Append Mode
Let us see how the append mode works.
Example: For this example, we will use the file created in the
previous example.
Python3
Output:
This is the write commandIt allows us to write in a particular
fileThis will add this line
There are also various other commands in file handling that are
used to handle various tasks:
rstrip(): This function strips each line of a file off spaces
from the right-hand side.
lstrip(): This function strips each line of a file off spaces
from the left-hand side.
It is designed to provide much cleaner syntax and exception
handling when you are working with code. That explains why it’s
good practice to use them with a statement where applicable. This is
helpful because using this method any files opened will be closed
automatically after one is done, so auto-cleanup.
Implementing all the functions in File Handling
In this example, we will cover all the concepts that we have seen
above. Other than those, we will also see how we can delete a file
using the remove() function from Python os module.
Python3
import os
def create_file(filename):
try:
with open(filename, 'w') as f:
f.write('Hello, world!\n')
print("File " + filename + " created successfully.")
except IOError:
print("Error: could not create file " + filename)
def read_file(filename):
try:
with open(filename, 'r') as f:
contents = f.read()
print(contents)
except IOError:
print("Error: could not read file " + filename)
def delete_file(filename):
try:
os.remove(filename)
print("File " + filename + " deleted successfully.")
except IOError:
print("Error: could not delete file " + filename)
if __name__ == '__main__':
filename = "example.txt"
new_filename = "new_example.txt"
create_file(filename)
read_file(filename)
append_file(filename, "This is some additional text.\n")
read_file(filename)
rename_file(filename, new_filename)
read_file(new_filename)
delete_file(new_filename)
Output:
File example.txt created successfully.
Hello, world!