10- Python file handling

10- Python file handling

Python provides several functions for creating, reading, updating, and deleting files.

open() function

The function used for working with files is open(fileName, mode). There are four different modes for opening a file:

  • "r" - Read - Default value. Opens a file for reading. Throws an error if the file does not exist.

  • "a" - Append - Opens a file for appending. Creates the file if it does not exist.

  • "w" - Write - Opens a file for writing. Creates the file if it does not exist.

  • "x" - Create - Creates the specified file. Throws an error if the file exists.

Assuming we have a text file named "data" in our directory folder:

read() mode

file = open('data.txt', 'r')

Print every line in the file

for each in file:
    print (each)

# This is for testing!

write() mode

file = open('data.txt','w')
file.write("The write command allows us to write in a particular file. ")
file.close()

Open the file again:

file = open('data.txt', 'r')
print(file.read())
# The write commandIt allows us to write in a particular file.

append() mode

file = open('data.txt','a')
file.write("This will add this sentence.")
file.close()

Open the file:

file = open('data.txt', 'r')
print(file.read())
# The write commandIt allows us to write in a particular file. This will add this sentence.

If you find this content helpful, please consider SUBSCRIBING to my channel for future updates.

If you would like to get the full video tutorial and a certificate, you can enroll in the course by following this link: https://www.udemy.com/course/python-for-researchers/?referralCode=886CCF5C552567F1C4E7

Did you find this article valuable?

Support Azad Rasul by becoming a sponsor. Any amount is appreciated!