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())