Much of the material in this document is taken from Appendix H.3 in the book A Primer on Scientific Programming with Python, 4th edition, by the same author, published by Springer, 2014.
Python applies the term directory instead of folder.
The equivalent of the Unix mkdir mydir
is
import os
os.mkdir('mydir')
Ordinary
files are created by the open
and close
functions in Python.
Suppose you want to make a subfolder under your home folder:
$HOME/python/project1/temp
but the intermediate folders python
and project1
do not exist.
This requires each new folder to be made separately by os.mkdir
,
or you can make all folders at once with os.makedirs
:
foldername = os.path.join(os.environ['HOME'], 'python',
'project1', 'temp')
os.makedirs(foldername)
With os.environ[var]
we can get the value of
any environment variable var
as
a string. The os.path.join
function joins folder names and a filename
in a platform-independent way.
The cd
command reads os.chdir
and cwd
is os.getcwd
:
origfolder = os.getcwd() # get name of current folder
os.chdir(foldername) # move ("change directory")
...
os.chdir(origfolder) # move back
The cross-platform mv
command is
os.rename(oldname, newname)
Unix wildcard notation can be used to list files. The equivalent of
ls *.py
and ls plot*[1-4]*.dat
reads
import glob
filelist1 = glob.glob('*.py')
filelist2 = glob.glob('plot*[1-4]*.dat')
The counterparts to ls -a mydir
and just ls -a
are
filelist1 = os.listdir('mydir')
filelist1 = os.listdir(os.curdir) # current folder (directory)
filelist1.sort() # sort alphabetically
The widely used constructions in Unix scripts for
testing if a file or folder
exist are if [ -f $filename ]; then
and
if [ -d $dirname ]; then
. These
have very readable counterparts in Python:
if os.path.isfile(filename):
inputfile = open(filename, 'r')
...
if os.path.isdir(dirnamename):
filelist = os.listdir(dirname)
...
Removing a single file is done with os.rename
, and a loop is
required for doing rm tmp_*.df
:
import glob
filelist = glob.glob('tmp_*.pdf')
for filename in filelist:
os.remove(filename)
The rm -rf mytree
command removes an entire folder tree.
In Python, the cross-platform valid command becomes
import shutil
shutil.rmtree(foldername)
It goes without saying that this command must be used with great care!
The cp fromfile tofile
construction applies shutil.copy
in Python:
shutil.copy('fromfile', 'tofile')
The recursive copy command cp -r
for folder trees
is in Python expressed by shell.copytree
:
shutil.copytree(sourcefolder, destination)