Introduction
In this article, we will discuss basic file operations in Python. How to do file handling in Python and file operation methods.
There are two types of file supported in Python,
- Text files
- Binary files
In Python, the following is the order of file operations,
- Open a file
- Read or write (perform operations) – Interestingly for reading and writing file in Python we don’t need to import an external library 🙂.
- Close the file
Let's explore each operation,
Open a file
- In python, we open the file using open() method
- open() returns file object
- syntax: file = open(filename, mode) where,
- filename: Name of the file if the file is in the current directory. Or the absolute path of the file
- mode: Optional parameter. Possible values for mode are
Mode |
Details |
r |
read. This is the default mode in case we didn’t specified mode parameter |
w |
write. Existing content will be overwritten. The new file will be created if a specified file does not exist |
a |
append the content to existing content in a given file. The new file will be created if a specified file does not exist |
r+ |
Opens the file for both operations – reading and writing |
x |
create. Creates the specified file either in the current directory or on a specified path. If a specified file already exists then returns an error |
Along with the mode parameter, we can also specify parameter for the file type, whether the file should be treated as a text or a binary file by specifying “t” or “b”
-
- file = open(“c:\Prasham\Blogs\Python\pythonfileoperations.txt”, ‘rt’)
Reading the file
There are multiple ways to read the file.
Reading the complete file, as one string,
Reading file line by line,
-
- for each in file:
- print (each)
-
- data = file.readlines()
- for line in data:
- print(line)
Read single line at a time,
Writing to the file
To write into a file, we need to open the file in write mode, otherwise, an exception will be there – io.UnsupportedOperation: not writable
Overwriting the existing file,
-
- file = open(“c:\Prasham\Blogs\Python\FileOperations\pythonfileoperations.txt”, ‘w’)
-
- file.write(“Writing to file – This is again the first line since it will overwrite the content of the existing file”)
Append the new content to existing content of the file,
-
- file = open(r”c:\Prasham\Blogs\Python\FileOperations\pythonfileoperations.txt”, ‘a’)
- file.write(“Writing to file – This is the second line”)
Writing multiple lines to file,
-
- file = open(“c:\Prasham\Blogs\Python\FileOperations\pythonfileoperations.txt”, ‘a’)
-
- lines_of_text = [“a line of text”, “another line of text”, “a third line”]
- file.writelines(lines_of_text)
- file.close()
Crating a new file,
-
- file = open(r”c:\Prasham\Blogs\Python\FileOperations\pythonfileoperations_new.txt”, ‘x’)
Deleting the file
To delete file, we import os module and use remove()
- mport os
- os.remove(r”c:\Prasham\Blogs\Python\FileOperations\pythonfileoperations_new.txt”)
-
-
-
- if os.path.exists(“demofile.txt”):
- os.remove(“demofile.txt”)
- else:
- print(“The file does not exist”)
Summary
These are the basic file operations discussed in this article, in next article we will discuss some more file handling details :)