Sabari M Sabari M
Updated date Feb 24, 2021
In this blog, We will see how to create, append, read, write a file in Python. File handling in python is very important.

File handling in python is very easy, it provides the in-built methods for creating, opening, reading, and writing files easily. The below code snippets shows how to create, open, write, append and, read the file in python.

Python File Modes:

Mode Description
r This mode is used to open a file for reading. This is the default mode.
w This Mode is used to open a file for writing. If a file doesn't exist, it will create a new file. If the file exists, it will delete and create a file.
x This mode is used to create a new file.
a This mode is used to open a file and append content. If a file doesn't exist, it will create a new file.
t This mode is used to open a file in text mode.
b This mode is used to open a file in binary mode.
+ This mode is used to open a file for reading and writing.

How To Create/Write a File in Python:

In the following example, we used the "SampleTextFile.txt" file name to create a new file and write the content to it. The open() method opens the file in write mode and write() method is used to write the content or text into the file. The close() is used to close the file after writing the content.

# Create a File in Python.

def main():
    # open the file and write the file contents. mode should be 'w+' (write). file will be created if doesn't exist
    fs = open("SampleTextFile.txt", "w+")

    # write() method is used to write the file content
    fs.write("TechieClues is a developer community\n")
    fs.write("Articles\n")
    fs.write("Blogs\n")
    fs.write("Tutorials\n")
    # close the file
    fs.close()


if __name__ == '__main__':
    main()

Output:

How to Append to a File in Python:

In the below example, we used the existing "SampleTextFile.txt" file to append the content to it. The open() method opens the file in append mode and write() method is used to append the content or text into the file. The close() is used to close the file after writing the content.

# Append a File in Python.

def main():
    # open the file and write the file contents. mode should be 'a+' (read). file will be created if doesn't exist
    fs = open("SampleTextFile.txt", "a+")

    # write() method is used to write the file content
    fs.write("Code Snippets\n")
    fs.write("Quick Answers\n")
    # close the file
    fs.close()


if __name__ == '__main__':
    main()

Output:

How to Read a File in Python:

In the following example, we used the existing "SampleTextFile.txt" file to read the whole content or text. The open() method opens the file in reading mode and read() method is used to read the entire content from the file. The print() is used to print the content to the console.

# Read a File in Python.

def main():
    # open the file and read the file contents. mode should be 'r' (read)
    fs = open("SampleTextFile.txt", "r")
    if fs.mode == 'r':
        # read() is used to read the file contents
        contents = fs.read()
        print(contents)


if __name__ == '__main__':
    main()

Output:

TechiClues is a developer community
Articles
Blogs
Tutorials
Code Snippets
Quick Answers

How to Read a File line by line in Python

In the following example, we used the existing "SampleTextFile.txt" file to read the whole content or text line by line in the file. The open() method opens the file in reading mode and readlines() method is used to read the entire content from the file. We used for loop to print the content line by line.

# Read a File line by line in Python.

def main():
    # open the file and read the file contents. mode should be 'r' (read)
    fs = open("SampleTextFile.txt", "r")

    # readlines() method is used to read the every lines of the file
    fs1 = fs.readlines()
    for t in fs1:
        print(t)


if __name__ == '__main__':
    main()


Output:

TechiClues is a developer community

Articles

Blogs

Tutorials

Code Snippets

Quick Answers

 

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 14+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!