Working with files in Python3

Introduction

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.

File Operation

There are mainly 3 file operations:

  • Open
  • Reading
  • Write

Deleting a file require os module and is not exactly correlated to this topic, thus will not be included

Opening files

In python there is a built-in function that we can take advantage of to work with files in python, which is open().

1
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

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Open with relative path

# Open data.txt for reading as text
file_1 = open("data.txt","rt")
file_1 = open("data.txt","r")
file_1 = open("data.txt")
# Open data.txt for appending as binary
file_2 = open("data.txt","ab")

# Open with absolute path
file_3 = open('/home/ravel/Documents/data.txt')

# Close all files
file_1.close();file_2.close();file_3.close()

Absolute and 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

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

1
2
UNIX: /home/ravel/Documents/program.txt
Windows: C:\users\ravel\Documents\data.txt

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

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:

1
2
Windows: cd (in windows cd also prints the current directory)
UNIX: pwd

An example of relative path is:

1
2
Windows: ..\Documents\data.txt
UNIX: ../Documents/data.txt

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.

Reading files

In order to read a file, you need to first open one, then with the result, there are 2 methods that you can use:

1
2
3
def read()
def read(number_of_chars: int)
def readline()

Read

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.

1
2
3
4
5
6
7
8
9
file = open("data.txt")

# Print the whole content
print(file.read())

# Print 10 first characters
print(file.read(10))

file.close()

Readline

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.

1
2
3
4
5
6
7
file = open("data.txt")

# Print 2 lines
print(file.readline())
print(file.readline())

file.close()

Creating and Writing file

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.

Creating file

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.

1
2
3
4
5
6
7
8
# Create a file
file = open("data.txt","x")

# Create a file if not exists
file = open("data.txt","w")

# Then you can work with the file
# don't forget to close it

Writing to 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

1
def open(data: str)
1
2
3
4
5
6
7
8
9
10
# Open file for appending
file = open("data.txt","a")
file.write("hello world")
file.close()

# Open file for overwriteing
file = open("data.txt","w")
# This will replace the file
file.write("helo")
file.close()
Author

Ravel Tanjaya

Posted on

2020-11-17

Updated on

2020-12-04

Licensed under