Newbie Question: Shell-like Scripting in Python?
Stephen Ferg
steve at ferg.org
Wed Oct 2 13:19:22 EDT 2002
Here are some routines that I've collected.
Use at your own risk -- they haven't been thoroughly used and tested.
And of course, to use them, you will have to import a few modules like
os, shutil, etc.
Note that you may have to un-wrap a few of the longer lines because
they were wrapped when the message was posted.
-- Steve Ferg (steve at ferg.org)
#--------------------------------------------------------------
# directory utilities
#--------------------------------------------------------------
def d_exists(d):
"""Return a boolean indicating whether directory d exists."""
return os.path.isdir(d)
def d_delete(d):
"""Delete a directory"""
return os.rmdir(d)
def d_create(d):
"""Create a directory if it doesn't exist"""
if d_exists(d): return
return os.mkdir(d)
def d_current():
"""return the current working directory"""
return os.getcwd()
def d_drive(new_driveletter = None):
"""Optionally, change the drive.
return the first letter of the drive specification of
the current directory.
"""
old_driveletter = os.path.splitdrive(os.getcwd())[0][:1]
if new_driveletter == None: pass
else:
new_driveletter = new_driveletter[:1] # use only the first letter
os.chdir( new_driveletter + ":") # change to the drive indicated by
the new_driveletter
return old_driveletter
#--------------------------------------------------------------
# file utilities
#--------------------------------------------------------------
def f_exists(f1):
if type(f1) != types.StringType: raise "Function requires argument of
StringType; received argument of " + `type(f1)`
f1 = os.path.normcase(f1)
return os.path.isfile(f1)
def f_list(f_spec, include_directory_name = 0):
"""return a list of all files that meet a certain filespec
"""
import glob
f_spec = os.path.normcase(f_spec)
list_of_files = glob.glob(f_spec)
return list_of_files
def f_delete(f1):
if type(f1) != types.StringType: raise "Function requires argument of
StringType; received argument of " + `type(f1)`
# f1 = os.path.normcase(f1)
#debugging -- if os.path.isfile(f1): print f1, "exists"
#debugging -- else: print f1, "does not exist"
if os.path.isfile(f1):
return os.remove(f1)
def f_rename(f1,f2):
"""Rename file with filename f1 to filename f2
"""
if type(f1) != types.StringType: raise "Function requires argument of
StringType; received argument of " + `type(f1)`
if type(f2) != types.StringType: raise "Function requires argument of
StringType; received argument of " + `type(f2)`
f1 = os.path.normcase(f1)
f2 = os.path.normcase(f2)
return os.rename(f1,f2)
def f_copy(f1,f2):
"""Copy a file with filename f1 to a file with filename f2,
F1 may contain wildcards (?*), in which case f2 is expected to
contain
a directory name, and all files matching the wildcard will be copied
into that directory.
"""
if type(f1) != types.StringType: raise "Function requires argument of
StringType; received argument of " + `type(f1)`
if type(f2) != types.StringType: raise "Function requires argument of
StringType; received argument of " + `type(f2)`
f1 = os.path.normcase(f1)
f2 = os.path.normcase(f2)
if match(f1, "?*") > -1:
# there were wildcards in the filespec
# that means that we're trying to copy a set of files to a directory
filename_list = f_list(f1)
for filename in filename_list:
try:
shutil.copy2(filename,f2)
except IOError, e:
print "\nException when attempting to copy \nfilespec: " + f1 +
"\nto dir..: " + f2 + "\n"
print "\nAttempting to copy \nfilename: " + filename + "\nto
dir..: " + f2 + "\n"
raise e
else: # only one file was requiested to be copied
if not f_exists(f1):
raise "Cannot copy file " + f1 + " to " + f2 + " -- file does not
exist."
try: return shutil.copy2(f1,f2)
except IOError, e:
print "\nIOError when attempting to copy \nfile: " + f1 + "\nto..:
" + f2 + "\n"
raise e
More information about the Python-list
mailing list