This notebook mirrors the Google Python Course: Utilities
The os and os.path modules include many functions to interact with the file system. The shutil module can copy files.
import os
os.listdir(".")
p = os.path.abspath('.')
p
f = os.path.join(p,'retest.py')
f
os.path.dirname(f)
os.path.basename(f)
os.path.exists(f)
newdir = os.path.join(p,"newdir")
os.path.exists(newdir)
os.mkdir(newdir)
os.path.exists(newdir)
newpath = os.path.join(newdir, 'a/b/c')
newpath
os.path.exists(newpath)
os.makedirs(newpath) ## create all needed directories
os.path.exists(newpath)
import shutil
shutil.copy('./retest.py', newdir)
os.listdir(newdir)
The commands module is a simple way to run an external command and capture its output. The commands module is no longer available in Python 3. Use the subprocess module instead.
import subprocess
(status, output) = subprocess.getstatusoutput("date")
status
output
subprocess.getoutput("date")
subprocess.getstatus("date")
def mygetstatus(cmd):
(status, output) = subprocess.getstatusoutput(cmd)
return status
mygetstatus('date')
mygetstatus('xsxsxs')
mygetstatus('ls /djdjd')
An exception represents a run-time error that halts the normal execution at a particular line and transfers control to error handling code. This section just introduces the most basic uses of exceptions. For example a run-time error might be that a variable used in the program does not have a value (NameError .. you've probably seen that one a few times), or a file open operation error because that a does not exist (IOError). (See [[http://docs.python.org/tut/node10.html][exception docs]])
joe
fd = open('somefile','r')
fd
Without any error handling code (as we have done thus far), a run-time exception just halts the program with an error message. That's a good default behavior, and you've seen it many times. You can add a "try/except" structure to your code to handle exceptions, like this:
try:
file = open("somefile",'r')
text = file.read()
file.close()
except FileNotFoundError:
print ("somefile not found. Sorry.")
print ("We keep on going.")
The try: section includes the code which might throw an exception. The except: section holds the code to run if there is an exception. If there is no exception, the except: section is skipped (that is, that code is for error handling only, not the "normal" case for the code). You can get a pointer to the exception object itself with syntax "except IOError, e: .. (e points to the exception object)".
A later notebook will explore exceptions in greater detail.
See urllib video from Socratica.
The module urllib provides url fetching -- making a url look like a file from which you can read. It contains four other modules: request, error, parse, and robotparse. Here are some useful methods from the request and parse modules.
import urllib
cs200url = 'https://zoo.cs.yale.edu/classes/cs200/index.html'
ufile = urllib.request.urlopen(cs200url)
type(ufile)
text = ufile.read()
type(text)
text[0:40]
len(text)
ufile.info()
type(ufile.info())
baseurl = ufile.geturl()
baseurl
The for loop and iterate through the url page, just as it can iterate through a file, line by line.
count = 0
for line in urllib.request.urlopen(cs200url):
count += 1
if count < 10:
print (line.decode('utf-8'), end='')
Now we try the urlretrieve() method.
urllib.request.urlretrieve(cs200url, "copyofcs200url")
with open('copyofcs200url','r') as f:
count = 0
for line in f:
count += 1
if count > 10:
pass
else:
print (line, end='')
urllib.parse.urljoin(baseurl, cs200url)
We can explore these objects uring the dir() command and related functions. These techniques are generally useful, beyond the urllib module.
dir(urllib.request)
ufile.length
ufile.status
dir(ufile)
ufile.url
for x in dir(ufile):
if x.startswith('_'):
pass
else:
print (x, '\t\t', getattr(ufile, x), '\n')
dir(os)
os.__doc__
End of Utilities notebook.
os.__name__
def f():
return 9
f()
dir(f)