Python OS Module

Introduction

Python is a powerful scripting language. So, why not use it to script Linux? The os module is Python’s answer to handling many Linux file operations.

The os module allows Python to perform many of the file and folder operations that you’d typically carry out in the Linux command line. It enable you to begin swapping out Bash for Python, which makes for a much cleaner and friendlier scripting experience.

Loading OS

The os module is a Python module like any other. In any script where you want to use it, you can use an import statement to pull it in.

import os

getcwd()

The getcwd() method returns the current working directory in the form of a string. You don’t need to pass it anything. It’s roughly the equivalent of pwd.

print(os.getcwd())

chdir()

chdir() is the Python equivalent of cd. Call the method and pass it the directory that you want to change to as a string.

os.chdir('/home/user/Documents')

It also supports using relative paths just like cd.

os.chdir('../Downloads')


listdir()

Using listdir() is very similar to running ls in Bash. There is one big difference, though, the return value.

Instead of printing out the contents of the directory, you receive them back as a list.

download_list = os.listdir('Downloads')
print(download_list)

Again, you can specify the directory, or it will list the contents of the current directory.

mkdir() and makedirs()

These two are exactly what you’d expect them to be. mkdir() works very similarly to the Linux command that you know. In this instance, it can take a second parameter to specify permissions.

os.mkdir('testdir', 755)

The makedirs() method works the same way, but can create directories recursively within each other, eliminating the need to run mkdir() multiple times.

os.makedirs('testdir2/something/somethingelse', 755)

Once again, you can specify the permissions, if you want to.

chmod()

This is once again a straightforward equivalent to it’s traditional counterpart. chmod() for Python takes two arguments. The first is the directory to change, and the second is the permissions to change to.

os.chmod('test', 774)

chown()

You can also change ownership with Python. chown() is the way you do it. The method is very similar to the Linux command, but it requires user and group ids to work.

os.chown('test', 1000, 1000)

That command will change the ownership of the “test” directory to the user with an id of 1000 and the group with an id of 1000.



remove() and removedirs()

remove() removes a single file that it’s passed. You can also pass it the complete path to the file.

os.remove('/home/user/Downloads/somefile')

To remove directories, use the removedirs() method. It will remove the specified directory and all of its contents.

os.removedirs('test_dir')

system()

So, the last method here is the most powerful and also the most dangerous. The system() method is cheating. It lets you run any Linux system command that you pass to it.

There are a couple of concerns here. First, to run the method, Python opens its own console. Using system() too much is inefficient and can hog resources. More importantly, system() can run anything. That means it can do some serious damage. If you’re going to use system() use it sparingly and under controlled circumstances.

Try running a command through system().

print(os.system(ls -lah))

You can also get sort of creative with it and even pipe together commands like you would in a normal terminal.

print(os.system(ps aux | grep firefox))

If you have Firefox running, the command will find the process and print out information on it.

Closing Thoughts

You’ve taken the first step towards scripting your Linux system with Python. There are more tools that Python offers, and things get get very complicated in this space. Popular automation tools like Ansible and Salt are made with Python, so there really isn’t a limit to what it can do.

Exercises

  1. Print your current directory using Python.
  2. Change into a different directory and print that one.
  3. Create a loop that iterates over the contents of a directory and prints each one individually.
  4. Create a new directory with a directory within it and another a step down.
  5. Change the permissions of your new directory so the owner can read write and execute, and everyone else can only read and execute.
  6. Delete the two subdirectories.
  7. Use Python to create a text file that lists all of the processes that your user is currently running.


Comments and Discussions
Linux Forum