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.
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:
- Open (or create) the file.
- Perform operations on the file, such as reading or writing to it.
- 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 writingfile = open("myfile.txt", "w")file.write("Hello world!")file.close()# Open existing file to read and print text contentfile = open("myfile.txt", "r")first_two_bytes = file.read(2)next_three_bytes = file.read(3)the_rest = file.read()print(first_two_bytes, next_three_bytes, the_rest, sep="\n")file.close()
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.
Files and the Command Line
Python files can be run as command line arguments using their filename.
python example_file.py# Alternatepython3 example_file.py
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.
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:
# example.pyimport imported_exampleif __name__ == '__main__':print('Example file: ' + __name__)print('Imported example file: ' + imported_example.__name__)
python example.py 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,
example.py, has its
__name__ set to
__main__ while the
__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 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.
- CaupolicanDiaz119 total contributions
- Anonymous contributorAnonymous contributor88 total contributions
- BrandonDusch580 total contributions
- Christine_Yang265 total contributions
- Anonymous contributorAnonymous contributor15 total contributions