Published Apr 12, 2022Updated Aug 11, 2023
Contribute to Docs

Files are named locations on the computer’s disk that permanently store information for future use of its data. They are used to permanently store data in non-volatile memory (e.g. hard disk) as opposed to volatile sources like Random Access Memory RAM, which loses its data when the computer is turned off.

File Handling

Handling files is a common feature that many languages use to work with the computer’s file system. In Python, file handling is possible and usually takes place in the following order:

  1. Open (or create) the file.
  2. Perform operations on the file, such as reading or writing to it.
  3. Close the file to free up any resources used.


The small example below demonstrates how one process of file handling could work:

# Create, or overwrite, a file and open for writing
file = open("myfile.txt", "w")
file.write("Hello world!")
# Open existing file to read and print text content
file = open("myfile.txt", "r")
first_two_bytes =
next_three_bytes =
the_rest =
print(first_two_bytes, next_three_bytes, the_rest, sep="\n")

In the first part of the code example, a plain text file named myfile.txt was created and opened in the "w" “write”-mode. Then a piece of text was written to the file and closed afterward.

In the next part, a few calls to the .read() method are assigned to some variables and then each one is printed on a new line:


Files and the Command Line

Python files can be run as command line arguments using their filename.

# Alternate

This executes a file’s inner __main__ environment variable, which then runs the code within. This code may include variable declarations, operations, and function calls.

More on __main__

Any time a .py file is run and interpreted, certain variables are set up and linked with the file. This includes __main__ environment variable, which is assigned as the file’s __name__ variable. All Python programs feature these variables, which can be verified as in the following example:

import imported_example
if __name__ == '__main__':
print('Example file: ' + __name__)
print('Imported example file: ' + imported_example.__name__)

When python is run on the command line, the following will be printed:

Example file: __main__
Imported example file: imported_example_file

The main file being run,, has its __name__ set to __main__ while the file’s __name__ is literally set to the name of the file.

More information about file methods in Python can be found below.


Allows the user to close an open file within the IDE.
Allows the user to read the contents of an open file and return the number of associated bytes.
Returns the first line of content from an open file.
Allows the user to delete a file if it exists.
Allows the user to delete a folder if it exists.
Allows the user to move the location of the file handle's reference point within an open file from one place to another.
Allows the user to resize the file to a given number of bytes when the file is accessed through the append mode.
Allows the user to delete a file path if it exists.
Allows the user to check if a file is writable or not. The function will return True if the file is writable and accessed in append or write mode, and False if it was accessed in read mode.
Adds additional text to a file when the file is opened in append mode.
Context Managers
Context managers allow users to perform operations within a certain overarching context.

All contributors

Looking to contribute?

Learn Python on Codecademy