Predictive Hacks

How to Interact with the Operating System in Python

Invest in you. Online courses from $11.99

os module

We will show some of the many things that we can do with files in Python. For these operations, we’ll be using functions provided by the os module which acts as a layer of abstraction between Python and the operating system. It allows us to interact with the underlying system without us knowing whether we’re working on a Windows, Mac or Linux. This means that you can write and test a script on one operating system like Windows and then run it on a different operating system like Linux. But one thing to watch out for, paths can be different across different operating systems. So whenever we’re using an absolute path in our code, we need to make sure we can provide alternatives for the platforms we want to support. The os module lets us do pretty much all the same tasks that we can normally do when working with files from the command line. We can change the file permissions and delete or rename files through our code. This means you can write scripts to do these operations for you automatically.


At this point, we will provide some examples of how you can remove or rename a file as well as to check if this file exists.

import os

# how to delete a file

# how to rename a file
os.rename("first_filename.txt", "final_file.txt")

# how to check if the file exists. It returns either True or False

Let’s provide some other examples of how you can get the size of the file, the last modified date and how you can convert Unix Timestamps to DateTime format

import os
# how to get the size of the file
# 56

# how to check when was the last time
# that this file modified. It returns the time in Unix Timestamp
# 1586518027.4692106

# Return the Unix Timestamp to Date Time
import datetime
mytimestamp = os.path.getmtime("myfile.txt") 
# datetime.datetime(2020, 4, 10, 14, 27, 7, 469211)

# How to check if the input is a file
# True
# False

In case we want to get more details about a file then we can use the os.stat() function which provides statistics of the file such as the st_size (size of the file), the st_atime (most recent access of the file), the st_mtime (the last modification time) and the st_ctime (creation time).


# os.stat_result(st_mode=33206, st_ino=9851624185481238, st_dev=2493502220, st_nlink=1, st_uid=0, st_gid=0, st_size=56,
# st_atime=1586518027, st_mtime=1586518027, st_ctime=1586517986)

# in case we weant to retrieve a specific information, like the creation time:
# 1586517986.6047087

Another cool feature of the functions is that we can work with both relative and absolute paths. In our examples, we’ve been using the relative file names without having to specify their full paths. In some cases, we may need to specify exactly where the file is to work with it in our script. This is where the abspath function can help.

import os

# get the absolute path
# 'C:\\Users\\gpipis\\myfile.txt'


We will provide some examples of how you can get the working directory, to change directory, to remove a directory and to list all the files of the directory

import os

# How to get your current working directory
# C:\Users\gpipis

# How to create a directory

# How to change a directory
# C:\Users\gpipis\new_dir

# How to remove an EMPTY directory

# How to list all the files/directories of the directories

# How you can get the full path

os.path.join("Documents", "My Pictures")
# 'Documents\\My Pictures'

# How to check if the file is a directory - Note it needs the full path
os.path.isdir(os.path.join("Documents", "My Pictures"))
# True

os.path.isdir(os.path.join("Documents", "mygzfile.tar.gz"))
# False

subprocess module

We can use the subprocess module if we want to run a system command as part of our Python script to accomplish a task, or use some functionality that doesn’t exist in the Python modules, neither built in or external.

Let’s provide some examples. Notice the returcode=0 means a success, where the returncode=2 means an error.

# Example of a "Hello World"
subprocess.check_output(['echo', 'hello world'])
#b'hello world\n'

# How to get the date 
import subprocess["date"])
# Fri Apr 10 15:18:20 GTBDT 2020
# CompletedProcess(args=['date'], returncode=0)

# How to get the list of files["ls"])

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

monte carlo integration in python

Monte Carlo Integration in Python

We will provide examples of how you solve integrals numerically in Python. Let’s recall from statistics that the mean value

3-day flash sale. Online courses start at $11.99