In This Article You Will Learn About Python File Handling
Python File Handling – Before moving ahead, let’s know a little bit about Python String format () Types
Generally, a file containing lots of coding lines and helps a language perform an action is a file.
Python File – Opening File is an important feature of Python. In Python, a file allows so many features like opening, reading, writing, editing, deleting, etc.
Python has a built-in function for working with files i.e., open () function.
open () function – Python works with files such as opening, reading, and writing, etc.
Syntax – open (filename, mode)
Parameter Values – filename – A file that is needed to work with. There are four different values of mode for opening a file - ‘r’ – Read – It is the default value. Opens a file for reading, if the file does not exist, return an error. ‘a’ – Append – It adds content to the existing file. Create a file, if the file does not exist. ‘w’ – Write – It writes the lines in the file. Create a file, if the file does not exist. ‘x’ – Create – It creates a file. Returns an error if the file already exists.
Apart from the above all, there are two additional ways to handle files in binary or text mode.
‘t’ – Text – It is the default value. Open a file in text mode. ‘b’ – Binary – It opens a file in binary mode. For example – images.
Let’s get started with Python key function –
Example – Using an open () function to open a file with the file name.
File = open (myfirstfile.py) print (File)
Note: If a file does not exist, it returns an error.
Example – Using an open () function to open a file with a file address.
File = open (write file address.py) print (File)
Note: If a file does not exist, it returns an error.
Example – Using mode ‘r’, to read a file.
File = open (myfirstfile.py, ‘r’) print(File.read())
Note: If a file does not exist, it returns an error.
Read Only a few lines of the file –
By default, mode ‘r’ returns whole text or content written in the file but by specifying number you can decide how much content to read.
Example – Using mode ‘r’ with a number to specify how much content to read
File = open (myfirstfile.py, ‘r’) print(File.read(21))
Read Lines – By using method readline(), you can return the file’s content line by line.
Example – Using mode ‘r’, to read a file.
File = open (myfirstfile.py, ‘r’) print(File.readline())
Similarly, as many as you call the readline() method, you will get that many lines.
Example – Using mode ‘r’, to read a file.
File = open (myfirstfile.py, ‘r’) print(File.readline()) print(File.readline()) print(File.readline())
For Loop – By using for loop, you can get all lines of file one by one.
File = open (myfirstfile.py, ‘r’) for z in File: print(z)
Here’s data stored in a file – myfirstfile.py
Close File – By using the method close(), you can close a file.
Example – Closing a file after reading it.
File = open (myfirstfile.py, ‘r’) print(File.readline()) File.close()
Note: Changes may not show in the file until you close it or open it again.
Writing an existing file – To write in an existing file, first open a file by using the open () function and append or add content by using mode ‘a’.
‘a’ – It will append lines at the end of the file in the existing file.
‘w’ – It will write the lines in the file.
Example – Adding a new line to file – myfirstfile.py.
File = open (myfirstfile.py, ‘a’) File.write(‘This new line is added.’) File.close() print(File)
Before adding a line – showing existing data of file
After adding line
Now read the file after adding lines.
File = open('myfirstfile.py', 'r') print(File.read())
Open a new file and overwrite the line.
File = open(newfile.py, ’w’) File.write(‘This line is added after deleting previous lines.’) File.close()
Now read the file after adding lines –
File = open (myfirstfile.py, ‘r’) print(File.read())
Note: ‘w’ method will change the whole content inside of a file.
Delete a file – By importing the Python OS module, we can delete a file.
remove() – It is a method that helps to delete a file.
import os os.remove(‘myfirstfile.py’)
Check if the file still exists –
By using the if… else condition, you can save to get an error, if the file is already deleted.
Example – Checking whether the file is still existing or is already deleted.
import os if os.path.exists("myfirstfile.py"): os.remove("myfirstfile.py") else: print("The file is no longer.")
Delete Folder – By using os.rmdir()
method, you can delete an entire folder.
Example – Deleting the whole folder.
import os
os.rmdir("pythonfilefolder")
Note: You can only remove empty folders.
If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.
Like us on