Python File Access

The file handling in Python plays an important role when the data needs to be stored permanently into the file. Python provides an inbuilt function for create, write, read, modify, and deleting files. There is no need for importing external Python library to read and write files. In Python, file processing takes place in the following order.

  1. Open a file which returns a file handle
  2. Use the file handle to perform actions
  3. Close the file handle

File Access Mode

The file access mode decide the mode in which the file has to be opened for, create, read, write, append, etc. The key function for working with files in Python is the open() method. The open() method takes two arguments: filename and mode.

open(filename,mode)

There are four types of mode, that Python provides and how files can be handle:

    "r", for reading.
  1. "w", for writing.
  2. "a", for appending.
  3. "r+", for both reading and writing

Python Write File

file = open('myFile.txt','w') file.write("Some thing write here..") file.close()

Python Read File


Python file operations
More on.... Python File Operations