- Reading and writing files is one of the most important and indispensable techniques if you are a professional programmer. Most software products today use this technique.
- Python is one of the most popular languages today, and of course it also provides methods to support file manipulation.
- And today I will introduce you how we handle files with python.
Open File
- The main function in handling files with python is the
open()
functionopen()
- The
open()
function takes two arguments: file name and mode. - There are 4 different file opening modes:
- “r”: Read – The default value This mode allows you to open a file for reading. Will return an error if the file does not exist.
- “a”: Append – Open a file to add it. If the file does not exist, it will automatically create a new file.
- “w”: Write – Open a file for writing. Create the file yourself if it doesn’t exist
- “x”: Create – Create a new file. Returns an error if the file already exists
- Also you can specify if the file is processed in binary or text format.
- “t”: Text – The default value
- “b”: Binary – Handling binary
- To open a file just for reading, you just need to specify the file name is enough
1 2 | f = open("demofile.txt") |
- The above code will be equivalent to:
1 2 | f = open("demofile.txt", "rt") |
Read File
- Suppose we have a
demo.text
file with the following content:
1 2 3 | Hello from another place |
- You open the file and use the
read()
function to read the file’s content
1 2 3 | f = open("demo.txt", "r") print(f.read()) |
- Result
- You can specify the number of characters read in the file:
1 2 3 | f = open("demo.txt", "r") print(f.read(5)) |
- Result
- You can specify to read the file by line.
1 2 3 | f = open("demo.txt", "r") print(f.readline()) |
- Result
- To read the whole file in line style. You use the loop
1 2 3 4 | f = open('demo.txt') for x in f: print(x) |
- Result
- Please close the file when you do not need to manipulate it anymore
1 2 3 4 5 | f = open('demo.txt') for x in f: print(x) f.close() |
Write File
Write an existing file
- To write a file you need to add an argument to the open function: – “a” – Append – Write at the end of the file
- “w” – Write – Overwrite the old file
- For example, I have a file
demowrite.txt
with the following content:
1 2 | This is a demo! |
- If recording the file using Append mode:
1 2 3 4 | f = open('demowrite.txt', 'a') f.write('text was write by append mode!') f.close() |
- Run the above code and reopen the file
demowrite.txt
you will see the following:
1 2 3 | This is a demo! text was write by append mode! |
- If we use the Write mode
1 2 3 4 | f = open('demowrite.txt', 'w') f.write('text was write by Write mode!') f.close() |
- Run the above code and reopen the file
demowrite.txt
you will see the following:
1 2 | text was write by Write mode! |
- Thus, the Write mode will overwrite the old data of the file, while the Append mode will keep the old data and write to the end of the file.
Create new File
- To create a new file in python, use the open () function with the following arguments:
- “x”: Create – Create a file, return an error if the file already exists.
- “a” – Append – Create a file if the file does not already exist
- “w” – Write – Create a file if it doesn’t already exist
- The following command will create a file
democreatefile.txt
1 2 | f = open("democreatefile.txt", "x") |
Delete File
- To delete a file, you need to import the OS Module and use its
remove()
function. - And to avoid the file does not exist, you should check before deleting:
1 2 3 4 5 6 | import os if os.path.exists("democreatefile.txt"): os.remove("democreatefile.txt") else: print("The file does not exist") |
Delete directory
- To delete directories, use the
rmdir()
function of the OS Module:
1 2 3 | import os os.rmdir("myfolder") |
Conclude
- OK so I have introduced you to some basic operations with files in Python.
- If you have any suggestions or suggestions, please leave them below to the commet.