What are Computer files?

Website Developer
7 min readJan 5, 2024

Files are storage containers for computers. They can be used for recording, storing, and reading data. It can be considered writing on paper; the computer is the paper, and the files are the writing. There are many different types of files with different kinds of purposes. There can be text, video, images, and many others. All these files are categorized separately on the computer’s storage system, usually by the extension, i.e., photo files are typically a .jpeg or .png, text files are generally .txt or .docx, and the list goes on for many other file types. Simply put, files store different kinds of information on computers. On a side note, many companies have custom file extensions specified for their custom file extensions specified for their product or software.


What are files used for in Python?

Python files are similar to other files in that they are used for reading, writing, and accessing storage. Python files are a specific type of file with Python code that can be opened with Python interpreters like VCS, PyCharm, and many others. There’s also a use for other files inside Python files. Many functions inside Python interact with different types of files. Files can be referenced, edited, deleted, etc., inside Python. This gives the user creative freedom to write programs and many other programs with different purposes. A few examples would be a program that mass deletes files off your computer. This would be helpful if, for example, your computer had a lot of junk files on it, and you wanted to clean it in bulk. Writing a program that mass deletes certain files could be possible. Another example would be a program that can write new content into files. If you, for example, wanted to edit many files in bulk, you could write a Python program that would do just that. Many functions in Python take advantage of the ability to modify files on your computer.

How are they used in Python?

Here’s an example of opening files in Python:


A = open("sample.txt", r)

Whatever “sample.txt” had inside of it.


The first string defines the variable A to open the file called “sample.txt”. This creates a reference point for Python to call back to. The “r” at the end of the line stands for “read’, which means Python will read the contents of the file. The second string is telling Python to output text from the variable “A”, which we defined to be “sample.txt” earlier. This means that printing the variable “A” will print out the contents of “sample.txt”.

This function would be massively helpful in a big project, where much of the information is stored in separate files. With Python, people can quickly call back to other files to display the stored data.

Here’s an example of adding content to an existing file in Python:


A = open("sample.txt", "a")
A.write("Sample content")

The output would modify the file by adding the content inside the quotes to the end of the original file.


The first line defines the variable “A” to open the file called “sample.txt”. This also creates a reference point for Python to open the file. The “a” at the end of the line stands for “append”, meaning new content will be added to the end of the file. The following line references the original file with “A.write”, and the .write at the end specifies that the file will have new content written inside of it. The text inside the quotes would be added to the end of the file. The last line closes the reference.

This program would also be beneficial in a big project if new data needs to be added to existing files. An earlier expression could collect information from the user and have that information written into a file. The file could be edited multiple times throughout the program and store many user inputs.

Here is a video detailing some other ways to handle files in Python.

-Advanced Uses

There are some more advanced uses for files within Python. These uses are harder to implement but come with many benefits. These functions are much more diverse and customizable in their uses. One advanced use would be to output the contents of the file line by line. This function could display large amounts of info and even modify how the information is structured / how it looks.


File = open("sample.txt", "r")
List = file.readlines();
For line in list

This program would output the contents of the file line by line. However, modifying the print command can change the output to look different.


The first line defines the variable “File” to open the sample text file with information inside it. The second line represents the variable “List” to the “readlines” command. This command reads the information in the sample file. The following line uses a for-loop. A for-loop repeats a command or function for every line. The for-loop causes Python to print out the information line for line. The last line will close the file.

This program is also useful when a lot of information needs to be displayed. This program is very similar to the previous one. The print functionality lets the user modify the output’s appearance when the command is run. Some example customizations could be adding specific phrases to the beginning or end of the printed lines. Another one could be numbering the lines. This program could be modified and integrated into a big project.

Another example of a more advanced function would be to mass delete files. A good use for this would be to clear out a large folder inside your computer without manually doing it. Clearing out files manually off your computer at a certain point becomes tedious, and Python allows you to automate the process and save time.


Import os
Path = r"C:\Parent\folder\SampleFolder\\"
For file_name in os.listdir(path):
File = path + file_name
If os.path.isfile(file)
Print('Deleting file:', file)

This program would remove the files inside the specified folder. The output would show a list of files being deleted as it continues going through the folder.


This function requires the user to import a custom module. Modules are extensions of Python made by people who can extend and change how Python works. The “os” module allows users to manipulate their operating system with commands. The first line of the program declares the module for Python to interpret. The second line defines the variable “Path” to the specific file path the user wishes to delete. The third line establishes a for-loop to sift through every file in the directory that the user specified earlier. The fourth line combines the file path with the file name, determining what the program needs to look for. The fifth line uses an if-statement and tells Python to delete the file using the “os.remove” command.

This is the most advanced program that I’ve written about. As I’ve written earlier, this program would be beneficial if a user needed to delete folders with many files inside. Once a folder is filled with enough files, clearing them out manually becomes tedious and time-consuming. This program will automate the process and save you a lot of time. This can be taken further by automatically clearing folders repeatedly after a specific time. For example, you can set this program to clear out your temp folder once every month. The temp folder on your computer stores temporary information from your browser, software, etc., and fills up very quickly. Deleting the contents of the temp folder will free up space and possibly even make your computer run smoother.

Common Problems

There are common problems when working with Python. Specifically, with files, there are common mistakes that users can make. One common one would be clashing file names. When files have clashing names, it could cause Python to struggle with determining which one to operate on. Some ways to fix this would be to specify the file type within the program and possibly change the name of the file that the user would want to operate on. This only applies when the file names are the same.

Another common problem could be not closing the file after it’s been modified within the code. Closing the file is required at the end of the operation because it could interfere with the other functions. For example, if you want to read, append another one immediately. You’d have to close the file at the end of both operations because they might interfere with each other and cause errors. A fix for this would be to close the files after you have finished your operations.

The last common mistake I’ll write about is the file not found or “FileNotFoundError” that may pop up when working with files in Python. This error pops up when Python can’t identify or locate the file you directed. The fix for this is to ensure you have specified the correct file path for Python to look for. Sometimes, you may need to select the entire file path, starting with the drive’s name, the user, etc.

The other common errors the user may encounter are common Python errors. Things like syntax and logic errors. Syntax errors are usually misspelling errors, where an argument or command is misspelled, causing the program to stop working. These are generally fixed by proofreading the file and ensuring everything is spelled correctly. Logic errors are where the files don’t have errors, but the program outputs a response it shouldn’t. These happen when people mistype the function to do something other than what the user wants. For example, if someone wrote two minus two instead of two plus two. The output would be zero, even though the user would expect a result of four. Similarly to the last one, this error is usually fixed by proofreading the file.


Files are used to store information on computers. Many kinds of files hold different types of information. Files containing text, audio, images, videos, etc., are constantly used by your computer to display the information inside. These files store and modify data in almost every task computers perform. In Python, files also store information, typically some code or program that can be executed. Python also can modify, create, or change files within the code. This allows the user to automate tasks efficiently. Using these functions to automate simple tasks can save precious time writing code.