Importing and using modules
The following section will explore how to tap into the rest of the Python standard library by way of importing modules.
The os
module
The
os
module provides functions that allow you to interact with the operating
system. To begin using functions from os
, you first need to import it
import os
Joining paths
os.path.join
allows you to combine strings with the appropriate
path separator
os.path.join('/path/to/data/', 'sub-001', 'ses-001')
This function call will return the path /path/to/data/sub-001/ses-001
.
Listing a directory
os.listdir
will list directory contents, similar to the ls
utility from
GNU coreutils
for entry in os.listdir('.'):
print(entry)
Walking a directory
If you want to crawl over a directory and every subdirectory from the command
line, you would run a command like ls -R
or find
. In Python, you can use
os.walk
for root, dirs, files in os.walk('.'):
for file in files:
fullfile = os.path.join(root, file)
print(fullfile)
Table of shell commands
Here's a quick list of commonly used Python functions and their corresponding GNU/Linux commands
Linux command | Python function |
---|---|
cd |
os.chdir |
pwd |
os.getcwd |
chmod |
os.chmod |
chown |
os.chown |
rm |
os.remove |
mv |
os.rename |
The shutil
module
Similar to the os
module,
shutil
contains more high-level functions for interacting with files and
directories
import shutil
Copying a file
To copy a file, you should use
shutil.copy2
.
This function accepts the source and destination files
shutil.copy2('/path/source.txt', '/path/destination.txt')
Python contains several functions for copying a file. There's
copy
,
copyfile
,
and
copy2
.
copy2
may be preferred under most circumstances since it preserves file
metadata.
Copying a directory
To copy an entire directory tree, you would typically use the cp -R
commnad. In
Python, you can use
shutil.copytree
.
This function accepts the source and destination directories
shutil.copytree('/path/source', '/path/destination')
Deleting a directory
To delete an entire directory from the command line, you would typically use
rm -r
and say a prayer. In Python, you would use
shutil.rmtree
.
This function accepts the directory to delete
Destructive command
You will not be asked for confirmation. Use at your own risk.
shutil.rmtree('/be/careful')
from x import y
Instead of importing a module and calling a function
import os
os.listdir()
you can use a from
statement to import only the function you need
from os import listdir
listdir()