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.
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.
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.
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
chdir() is the Python equivalent of
cd. Call the method and pass it the directory that you want to change to as a string.
It also supports using relative paths just like
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.
makedirs() method works the same way, but can create directories recursively within each other, eliminating the need to run
mkdir() multiple times.
Once again, you can specify the permissions, if you want to.
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.
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.
To remove directories, use the
removedirs() method. It will remove the specified directory and all of its contents.
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
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.
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.
- Print your current directory using Python.
- Change into a different directory and print that one.
- Create a loop that iterates over the contents of a directory and prints each one individually.
- Create a new directory with a directory within it and another a step down.
- Change the permissions of your new directory so the owner can read write and execute, and everyone else can only read and execute.
- Delete the two subdirectories.
- Use Python to create a text file that lists all of the processes that your user is currently running.