Get The Last Modification Time Of A File

# ## get the modification time of a file # import os mtime=os.path.getmtime(file) # mtime is a float, need to do something to print it with a string strmtime = str(mtime) # can examine intermediate value print ('mtime '+strmtime)

Get the Size of a File

# ## get the size of a file # import os size=os.path.getsize(file)

read input interactively

# ## read input line into variable num # num = raw_input("Enter a number: ") # Python 2.x print("The number is: %s" % num)

Read a File quickly into a variable

# ## slurp a file -- read all of the bytes into variable buf # buf = open(file,'r').read()

Get The Current Directory

# ## get the current directory # import os pwd = os.getcwd()

Creating Directories

# ## To avoid umask calculations, ## save the current umask while setting the umask to 000, ## make the directory, then set the umask back to what it had been # import os newdir = "./new/" if os.path.isdir(newdir): print "newdir exists" else: try: original_umask = os.umask(0) os.makedirs(newdir, 0755) finally: os.umask(original_umask)

Copy Files To The Directory

# populate the dir # copy2 preserves mtime, and dst can be a directory import shutil dst = newdir + file shutil.copy2(file, dst) dst = newdir + file + ".1" shutil.copy2(file, dst)

Read A Directory

# ## read a directory $dir ( ignoring . and .. ) # import os files = os.listdir(newdir)

Recursively Read a Directory

# ## recursively read a directory, startdir finding all files ## at the end all directory names are in dirs, all files are in files # import os dirs = [] files = [] dirs.append(startdir) for dir in dirs: print('dir ' + dir) list = os.listdir(dir) for name in list: fullpath = dir + name print('fullpath ' + fullpath) if os.path.isdir(fullpath): print("is a dir") dirs.append(fullpath + '/') if os.path.isfile(fullpath): print("is a file") files.append(fullpath)

Breaking out or "short circuiting" a loop

# ## break out of a loop # import subprocess cmd = ["/usr/sbin/dmidecode"] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) for line in proc.stdout.readlines(): strippedline = line.rstrip() #Product Name: BladeCenter # strippedline.split(':') import re pattern = 'Product Name:' if re.search(pattern, strippedline): print strippedline name, var = strippedline.partition(":")[::2] print("var " + var) break

Determining The PID of a Child Process

# ## determining the PID of a child process # import subprocess cmd = ["/usr/sbin/dmidecode"] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) pid = proc.pid print(pid)

get the loginID

# ## get the loginID (unix only) # import os uid = os.getuid() import pwd name = pwd.getpwuid(uid)[0] print("process owner is " + name) # ## was it run via sudo? # try: user = os.environ['SUDO_USER'] except KeyError: # if not defined it thows a KeyError if name == "root": user = name else: print "This script must be run via sudo!" exit

get the hostname

# ## get the hostname # import socket hostname = socket.gethostname() print("hostname is %s" % hostname)

concatenate the elements of an array, separated by $delimiter

# ## concatenate the elements of an array, separated by $delimiter # delimiter = '-' list = ['apple', 'cherry', 'pumpkin'] strlist = delimiter.join(list) print("joined list: %s" % strlist)

print using a "here" document

# ## print using a "here" document -- print out html code without lots of quoting games # block = (""" <SCRIPT LANGUAGE=JAVASCRIPT> <!-- document.write('<img src="/cgi-bin/get-img.cgi?'+document.referrer+'"alt="logo" height=103 width=419>') // --> </script> <NOSCRIPT> <img src="/cgi-bin/get-img.cgi?noscript" alt="logo" height=103 width=419> </NOSCRIPT> """) print(block.format(name = name))