Working with files in Python3
Python is a very popular general purpose programming language. One of the most popular use case of python is as a system scripting language, it allows the automation of repetitive task to increase the efficiency of the overall workflow. When we are talking about automating system task, a type of automation that will ofter done with python is file operation automation, this include file manipulation, directory manupulation, copying and also moving files.
As working with files is a very fundamental part of doing scripting and automation with python, in this article, we are going to take a deeper look at how to work with files in python.
There are mainly 3 file operations:
Deleting a file require os module and is not exactly correlated to this topic, thus will not be included
In python there is a built-in function that we can take advantage of to work with files in python, which is open().
def open(filename: str,mode: str)
The first parameter that is accepted by open is filename, it accepts both relative and absolute path.
The second parameter of the open method accept a string with the following details:
- “x” Create the file (error if exists)
- “w” Open for writing (create if not exists)
- “a” Open for appending to the end of file (create if not exists)
- “r” Read (error if not exists) - Default
As the second parameter is string, you can combine it with another character that represent in what mode the data is read as,
- “t” Text mode, for reading text based data or code - Default
- “b” Binary mode, only used for reading files such as movie, image or other form of binary files.
If you are using the default mode, then the letter can be ommited
After you finish working with your files, you should always close it, sometimes changes only reflect on the file after the file is closed.
You can use .close() builtin method to close a file
# Open with relative path
Path is term that is used to point to a certain location inside a filesystem. Generally, there are 2 ways to point to a specific file or directory, which is
- Absolute Path
- Relative Path
Absolute path will look for a certain file or directory from the root of a certain partition.
For example on Linux and Mac, we have / as the root of the filesystem while on Windows, we ussually have letter based partition such as C:\ or D:\.
And example of absolute path is
Please be aware that on Unix based system (ex: Linux & Mac) the delimiter in a path is marked by / while on Windows it is marked with \
Relative path is kind of the opposite of absolute path, rather than writing full address to a certain file or directory, we tell the computer to find the data relative to the current directory.
An easy what to think about relative and absolute path is by using this analogy:
- Imagine that you are going to your friend’s house
- Your friend give you the whole address including city,state and country (ex: downstreet, jakarta, Indonesia)
- That is an absolute path, you have the neccesary information to get there even if you don’t know that person.
- But if your friend give you an address which state “i’m in the same neighbourhood as you, but i’m on number 32”
- That is a relative path, you need to have some context and information about where your friend, in order to fully understand how to get there.
In computers, relative path is calculated from the currect working directory,
to find the currect directory, you can use these commands:
Windows: cd (in windows cd also prints the current directory)
An example of relative path is:
Both of the commands above goes back one directory and then go to the documents folder relative to it and finally to the data.txt
An important reminder is that . and .. in path means current and parent directory.
In order to read a file, you need to first open one, then with the result, there are 2 methods that you can use:
The read method will read the whole file content and store it as a string, there is also a variation of read that allows user to set how many character that they want to read from the file.
file = open("data.txt")
Readline will read a single line (or technically speaking, the function will seperate the string on \n) of the data and keep track of the line read by readline, we can use loops if we want to read all.
file = open("data.txt")
To create or write to a file, we will still need to first open a file but in this case, we’ll need to pass special parameter to mode.
In order to create a file in python, we’ll need one of these 3 parameters:
- “x” Create
- “a” Append
- “w” write
For more information about these options, please refer to “opening file” in the section above
to create file, you just need to call open on the appropriate function with the appropriate mode, you can discard the return value if you just want to create a file but you do not want to work with it.
# Create a file
When we are writing to a file with python,we need to first open the file and pass the appropriate mode, there are 2 different mode that we can use:
- “a” non-destructive (append to end of file)
- “w” destructive (overwrite the file)
After opening the file, we can write to it with the write method, if accepts a string and will put it to the file
def open(data: str)
# Open file for appending
Working with files in Python3