Python File Handling
File handling in python lets you open, read, write, append, modify files.
Note: For the sake of this tutorial, we will assume that your text file and python code are present in the same directory.
Before we perform any operation on file, we need to open the file. This is done using the open() function.
Example: lets say we have a text file (someText.txt) with some content in it. The open() function creates a file object with read() method for reading the content.
file = open("someText.txt")
print(file.read())
Output:
lorem ipsum
In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content.
There are various modes in which we can open files.
· read (r): This mode opens the file for reading only and gives an error if the file does not exist. This is the default mode if no mode is passed as a parameter.
· write (w): This mode opens the file for writing only and creates a new file if the file does not exist.
· append (a): This mode opens the file for appending only and creates a new file if the file does not exist.
· create (x): This mode creates a file and gives an error if the file already exists.
Apart from these modes we also need to specify how the file must be handled:
· text (t): Used to handle text files.
· binary (b): used to handle binary files (images).
Read/Write Files
A. Create a File:
Creating a file is primarily done using the create (x) mode.
Example:
file = open("Text.txt", "x")
Output:
A file named Text.txt is created with no content.
B. Write onto a File:
This method writes content onto a file.
Example:
file = open("Text.txt", "w")
file.write("This is an example of file creation.")
file.close
Output:
This is an example of file creation.
If the file already exists with some content of its own, then this mode overwrites it.
Example:
file = open("Text.txt", "w")
file.write("This is overwritten text.")
file.close
Output:
This is overwritten text.
C. Read a File:
This method allows us to read the contents of the file.
Example:
file = open("Text2.txt", "r")
print(file.read())
file.close
Output:
Hello, I’m a Potato.
D. Append a File:
This method appends content into a file.
Example:
file = open("newText.txt", "a")
file.write("This is an example of file appending.")
file.close
Output:
This is an example of file appending.
However, If the file exists already with some content in it, then the new content gets appended.
Example:
file = open("newText.txt", "a")
file.write(" Hello, I'm appending")
file.close
Output:
This is an example of file appending. Hello, I'm appending
0 Comments