Handling files with python

Tram Ho

  • 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() function open()
  • 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

  • The above code will be equivalent to:

Read File

  • Suppose we have a demo.text file with the following content:

  • You open the file and use the read() function to read the file’s content

  • Result

  • You can specify the number of characters read in the file:

  • Result

  • You can specify to read the file by line.

  • Result

  • To read the whole file in line style. You use the loop

  • Result

  • Please close the file when you do not need to manipulate it anymore

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:

  • If recording the file using Append mode:

  • Run the above code and reopen the file demowrite.txt you will see the following:

  • If we use the Write mode

  • Run the above code and reopen the file demowrite.txt you will see the following:

  • 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

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:

Delete directory

  • To delete directories, use the rmdir() function of the OS Module:


  • 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.


Share the news now

Source : Viblo