CS 200: Utilities in Python

This notebook mirrors the Google Python Course: Utilities

File System -- os, os.path, shutil

The os and os.path modules include many functions to interact with the file system. The shutil module can copy files.

  • os module docs
  • filenames = os.listdir(dir) -- list of filenames in that directory path (not including . and ..). The filenames are just the names in the directory, not their absolute paths.
  • os.path.join(dir, filename) -- given a filename from the above list, use this to put the dir and filename together to make a path
  • os.path.abspath(path) -- given a path, return an absolute form, e.g. /home/nick/foo/bar.html
  • os.path.dirname(path), os.path.basename(path) -- given dir/foo/bar.html, return the dirname "dir/foo" and basename "bar.html"
  • os.path.exists(path) -- true if it exists
  • os.mkdir(dir_path) -- makes one dir, os.makedirs(dir_path) makes all the needed dirs in this path
  • shutil.copy(source-path, dest-path) -- copy a file (dest path directories should exist)
In [1]:
import os
In [2]:
os.listdir(".")
Out[2]:
['.swirl',
 '__pycache__',
 'nn',
 '.old2018',
 'sh',
 'sklearn',
 '.old2017',
 'f0201.py',
 'f0831.py',
 'cs200.ipynb',
 'notebook.py',
 '0201.html',
 '0203.html',
 'psource.py',
 'collatz.py',
 'python.html',
 '0201nb.html',
 'google-python-exercises',
 '0201.script',
 '0201nb.ipynb',
 '0208.html',
 '0203.script',
 '0210.html',
 '0215.html',
 'f0909.py',
 'Introduction.ipynb',
 'Strings.ipynb',
 '0208.script',
 'Introduction.html',
 'cs200.html',
 'Strings.html',
 'testfile',
 'Lists.html',
 '0210.script',
 'Lists.ipynb',
 'Sorting.html',
 'Sorting.ipynb',
 'newdir',
 'recursion.py',
 'fib.py',
 'DictFiles.ipynb',
 'RegExp.ipynb',
 'Utilities.ipynb',
 '0217.html',
 '0224.html',
 'linux.words',
 'DictFiles.html',
 'puzzle.py',
 'Recursion.html',
 'Recursion.ipynb',
 'listcomp.py',
 '0217.script',
 '0301.html',
 'gullible.png',
 'Listcomp.html',
 'knapsack.py',
 'Listcomp.ipynb',
 '0224.script',
 'retest.py',
 'RegExp.html',
 'mt.py',
 'is-this-going.jpg',
 'Utilities.html',
 'hw3a.pyc',
 'Hw3.html',
 'Hw3.ipynb',
 '.old2016',
 '.old2019',
 'DeepRecursion.html',
 'testfile2',
 'testfile3',
 '.old2020',
 'DeepRecursion.ipynb',
 '.old2020f',
 'Oop.html',
 'decorators.py',
 'ls.output',
 'testfile11',
 '.ipynb_checkpoints',
 '.#0301.script',
 'Oop.ipynb',
 'exceptions.html',
 'exceptions.py',
 'iterators.py',
 '0301.html~',
 'Exceptions.html',
 'Exceptions.ipynb',
 'Decorators.html',
 '0301.script',
 'Decorators.ipynb',
 '#0301.script#',
 'fire',
 'calc.py',
 'practicemidterm1ans.pdf',
 'oop.py',
 'cryptopals',
 'practicemidterm1.pdf',
 'midterm1review.pdf',
 'mt.script',
 'mt.script.answer',
 'hamlet.py',
 'stack.py',
 'trees.py',
 'graph.py',
 'Iterators.html',
 'Iterators.ipynb',
 'DataStructures.ipynb',
 'Matplotlib.html',
 'Matplotlib.ipynb',
 'f0217.py',
 'DataStructures.html',
 'hw4a.pyc',
 ',',
 'pvm.py',
 'script',
 'script2',
 'bytecode.py',
 'PVM.ipynb',
 'PVM.html',
 'hw5a.pyc',
 'practicemidterm2ans.pdf',
 'Analysis.ipynb',
 'practicemidterm2.pdf',
 'Analysis.html',
 'sql.py',
 'SQL.ipynb',
 'mydb.db',
 'SQL.html',
 'SkiennaSorting.pdf',
 'PVM(1).html',
 'file1',
 'file2',
 'oop.html',
 'crypto.html',
 'Crypto.html',
 'Crypto.ipynb',
 'test.png',
 'ML.ipynb',
 'script3',
 'ML.html',
 'prep_terrain_data.py',
 'class_vis.py']
In [3]:
p = os.path.abspath('.')
In [4]:
p
Out[4]:
'/home/httpd/html/zoo/classes/cs200/lectures'
In [5]:
f = os.path.join(p,'retest.py')
In [6]:
f
Out[6]:
'/home/httpd/html/zoo/classes/cs200/lectures/retest.py'
In [7]:
os.path.dirname(f)
Out[7]:
'/home/httpd/html/zoo/classes/cs200/lectures'
In [8]:
os.path.basename(f)
Out[8]:
'retest.py'
In [9]:
os.path.exists(f)
Out[9]:
True
In [10]:
newdir = os.path.join(p,"newdir")
In [11]:
os.path.exists(newdir)
Out[11]:
True
In [12]:
os.mkdir(newdir)
---------------------------------------------------------------------------
FileExistsError                           Traceback (most recent call last)
<ipython-input-12-268f148bb6ba> in <module>
----> 1 os.mkdir(newdir)

FileExistsError: [Errno 17] File exists: '/home/httpd/html/zoo/classes/cs200/lectures/newdir'
In [13]:
os.path.exists(newdir)
Out[13]:
True
In [14]:
newpath = os.path.join(newdir, 'a/b/c')
In [15]:
newpath
Out[15]:
'/home/httpd/html/zoo/classes/cs200/lectures/newdir/a/b/c'
In [16]:
os.path.exists(newpath)
Out[16]:
True
In [17]:
os.makedirs(newpath)  ## create all needed directories
---------------------------------------------------------------------------
FileExistsError                           Traceback (most recent call last)
<ipython-input-17-47183f228a10> in <module>
----> 1 os.makedirs(newpath)  ## create all needed directories

/usr/lib64/python3.8/os.py in makedirs(name, mode, exist_ok)
    221             return
    222     try:
--> 223         mkdir(name, mode)
    224     except OSError:
    225         # Cannot rely on checking for EEXIST, since the operating system

FileExistsError: [Errno 17] File exists: '/home/httpd/html/zoo/classes/cs200/lectures/newdir/a/b/c'
In [18]:
os.path.exists(newpath)
Out[18]:
True
In [19]:
import shutil
In [20]:
shutil.copy('./retest.py', newdir)
Out[20]:
'/home/httpd/html/zoo/classes/cs200/lectures/newdir/retest.py'
In [22]:
os.listdir(newdir)
Out[22]:
['a', 'retest.py']

Running External Processes -- commands subprocess

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.

  • subprocess module docs

  • (status, output) = subprocess.getstatusoutput(cmd) -- runs the command, waits for it to exit, and returns its status int and output text as a tuple. The command is run with its standard output and standard error combined into the one output text. The status will be non-zero if the command failed. Since the standard-err of the command is captured, if it fails, we need to print some indication of what happened.

  • output = subprocess.getoutput(cmd) -- as above, but without the status int.

  • There is no subprocess.getstatus() but we can define a similar function.

  • If you want more control over the running of the sub-process, see the "popen2" module (http://docs.python.org/lib/module-popen2.html)

  • There is also a simple os.system(cmd) which runs the command and dumps its output onto your output and returns its error code. This works if you want to run the command but do not need to capture its output into your python data structures.

In [23]:
import subprocess
In [24]:
(status, output) = subprocess.getstatusoutput("date")
In [26]:
status
Out[26]:
0
In [27]:
output
Out[27]:
'Mon 01 Mar 2021 02:46:52 PM EST'
In [28]:
subprocess.getoutput("date")
Out[28]:
'Mon 01 Mar 2021 02:48:24 PM EST'
In [6]:
subprocess.getstatus("date")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-60a9b3560cfb> in <module>
----> 1 subprocess.getstatus("date")

AttributeError: module 'subprocess' has no attribute 'getstatus'
In [29]:
def mygetstatus(cmd):
    (status, output) = subprocess.getstatusoutput(cmd)
    return status
In [30]:
mygetstatus('date')
Out[30]:
0
In [31]:
mygetstatus('xsxsxs')
Out[31]:
127
In [33]:
mygetstatus('ls /djdjd')
Out[33]:
2

Exceptions

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]])

In [34]:
joe
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-34-2a763a0e08e7> in <module>
----> 1 joe

NameError: name 'joe' is not defined
In [35]:
fd = open('somefile','r')
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-35-a893074bc87a> in <module>
----> 1 fd = open('somefile','r')

FileNotFoundError: [Errno 2] No such file or directory: 'somefile'
In [37]:
fd
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-37-c1b1d8137e2e> in <module>
----> 1 fd

NameError: name 'fd' is not defined

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:

In [38]:
try:
    file = open("somefile",'r')
    text = file.read()
    file.close()
except FileNotFoundError:
    print ("somefile not found.  Sorry.")
print ("We keep on going.")
somefile not found.  Sorry.
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.

HTTP -- urllib and urlparse

Video:

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.

  • urllib module docs
  • ufile = urllib.request.urlopen(url) -- returns a file like object for that url
  • text = ufile.read() -- can read from it, like a file (readlines(), for loops, etc. also work)
  • info = ufile.info() -- the meta info for that request. info.gettype() is the mime time, e.g. 'text/html'
  • baseurl = ufile.geturl() -- gets the "base" url for the request, which may be different from the original because of redirects
  • urllib.request.urlretrieve(url, filename) -- downloads the url data to the given file path
  • urllib.parse.urljoin(baseurl, url) -- given a url that may or may not be full, and the baseurl of the page it comes from, return a full url. Use geturl() above to provide the base url.
In [39]:
import urllib
In [40]:
cs200url = 'https://zoo.cs.yale.edu/classes/cs200/index.html'
In [41]:
ufile = urllib.request.urlopen(cs200url)
In [42]:
type(ufile)
Out[42]:
http.client.HTTPResponse
In [43]:
text = ufile.read()
In [44]:
type(text)
Out[44]:
bytes
In [46]:
text[0:40]
Out[46]:
b'<HTML>\n<HEAD>\n<TITLE>CPSC 200 - Introduc'
In [47]:
len(text)
Out[47]:
2749
In [48]:
ufile.info()
Out[48]:
<http.client.HTTPMessage at 0x7f4244267250>
In [49]:
type(ufile.info())
Out[49]:
http.client.HTTPMessage
In [50]:
baseurl = ufile.geturl()
In [51]:
baseurl
Out[51]:
'https://zoo.cs.yale.edu/classes/cs200/index.html'

The for loop and iterate through the url page, just as it can iterate through a file, line by line.

In [52]:
count = 0
for line in urllib.request.urlopen(cs200url):
    count += 1
    if count < 10:
        print (line.decode('utf-8'), end='')
<HTML>
<HEAD>
<TITLE>CPSC 200 - Introduction to Information Systems</TITLE>
<BASE TARGET="_top">  <!-- Prevent Classes*V2 from branding links -->
</HEAD>
<BODY>
<dl>
  <dt><h1>CPSC 200 - Introduction to Information Systems</h1>
  <dt><h2>SPRING 2021</h2>

Now we try the urlretrieve() method.

In [53]:
urllib.request.urlretrieve(cs200url, "copyofcs200url")
Out[53]:
('copyofcs200url', <http.client.HTTPMessage at 0x7f42442061c0>)
In [54]:
with open('copyofcs200url','r') as f:
    count = 0
    for line in f:
        count += 1
        if count > 10:
            pass
        else:
            print (line, end='')
<HTML>
<HEAD>
<TITLE>CPSC 200 - Introduction to Information Systems</TITLE>
<BASE TARGET="_top">  <!-- Prevent Classes*V2 from branding links -->
</HEAD>
<BODY>
<dl>
  <dt><h1>CPSC 200 - Introduction to Information Systems</h1>
  <dt><h2>SPRING 2021</h2>
</dl>
In [55]:
urllib.parse.urljoin(baseurl, cs200url)
Out[55]:
'https://zoo.cs.yale.edu/classes/cs200/index.html'

We can explore these objects uring the dir() command and related functions. These techniques are generally useful, beyond the urllib module.

In [56]:
dir(urllib.request)
Out[56]:
['AbstractBasicAuthHandler',
 'AbstractDigestAuthHandler',
 'AbstractHTTPHandler',
 'BaseHandler',
 'CacheFTPHandler',
 'ContentTooShortError',
 'DataHandler',
 'FTPHandler',
 'FancyURLopener',
 'FileHandler',
 'HTTPBasicAuthHandler',
 'HTTPCookieProcessor',
 'HTTPDefaultErrorHandler',
 'HTTPDigestAuthHandler',
 'HTTPError',
 'HTTPErrorProcessor',
 'HTTPHandler',
 'HTTPPasswordMgr',
 'HTTPPasswordMgrWithDefaultRealm',
 'HTTPPasswordMgrWithPriorAuth',
 'HTTPRedirectHandler',
 'HTTPSHandler',
 'MAXFTPCACHE',
 'OpenerDirector',
 'ProxyBasicAuthHandler',
 'ProxyDigestAuthHandler',
 'ProxyHandler',
 'Request',
 'URLError',
 'URLopener',
 'UnknownHandler',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '__version__',
 '_cut_port_re',
 '_ftperrors',
 '_have_ssl',
 '_localhost',
 '_noheaders',
 '_opener',
 '_parse_proxy',
 '_proxy_bypass_macosx_sysconf',
 '_randombytes',
 '_safe_gethostbyname',
 '_splitattr',
 '_splithost',
 '_splitpasswd',
 '_splitport',
 '_splitquery',
 '_splittag',
 '_splittype',
 '_splituser',
 '_splitvalue',
 '_thishost',
 '_to_bytes',
 '_url_tempfiles',
 'addclosehook',
 'addinfourl',
 'base64',
 'bisect',
 'build_opener',
 'contextlib',
 'email',
 'ftpcache',
 'ftperrors',
 'ftpwrapper',
 'getproxies',
 'getproxies_environment',
 'hashlib',
 'http',
 'install_opener',
 'io',
 'localhost',
 'noheaders',
 'os',
 'parse_http_list',
 'parse_keqv_list',
 'pathname2url',
 'posixpath',
 'proxy_bypass',
 'proxy_bypass_environment',
 'quote',
 're',
 'request_host',
 'socket',
 'ssl',
 'string',
 'sys',
 'tempfile',
 'thishost',
 'time',
 'unquote',
 'unquote_to_bytes',
 'unwrap',
 'url2pathname',
 'urlcleanup',
 'urljoin',
 'urlopen',
 'urlparse',
 'urlretrieve',
 'urlsplit',
 'urlunparse',
 'warnings']
In [57]:
ufile.length
Out[57]:
0
In [10]:
ufile.status
Out[10]:
200
In [14]:
dir(ufile)
Out[14]:
['__abstractmethods__',
 '__class__',
 '__del__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__enter__',
 '__eq__',
 '__exit__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__next__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '_abc_impl',
 '_checkClosed',
 '_checkReadable',
 '_checkSeekable',
 '_checkWritable',
 '_check_close',
 '_close_conn',
 '_get_chunk_left',
 '_method',
 '_peek_chunked',
 '_read1_chunked',
 '_read_and_discard_trailer',
 '_read_next_chunk_size',
 '_read_status',
 '_readall_chunked',
 '_readinto_chunked',
 '_safe_read',
 '_safe_readinto',
 'begin',
 'chunk_left',
 'chunked',
 'close',
 'closed',
 'code',
 'debuglevel',
 'detach',
 'fileno',
 'flush',
 'fp',
 'getcode',
 'getheader',
 'getheaders',
 'geturl',
 'headers',
 'info',
 'isatty',
 'isclosed',
 'length',
 'msg',
 'peek',
 'read',
 'read1',
 'readable',
 'readinto',
 'readinto1',
 'readline',
 'readlines',
 'reason',
 'seek',
 'seekable',
 'status',
 'tell',
 'truncate',
 'url',
 'version',
 'will_close',
 'writable',
 'write',
 'writelines']
In [58]:
ufile.url
Out[58]:
'https://zoo.cs.yale.edu/classes/cs200/index.html'

Neat trick for analyzing a module's properties and methods.

In [33]:
for x in dir(ufile):
    if x.startswith('_'):
        pass
    else:
        print (x, '\t\t', getattr(ufile, x), '\n')
begin 		 <bound method HTTPResponse.begin of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

chunk_left 		 UNKNOWN 

chunked 		 False 

close 		 <bound method HTTPResponse.close of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

closed 		 False 

code 		 200 

debuglevel 		 0 

detach 		 <built-in method detach of HTTPResponse object at 0x7f9764df25b0> 

fileno 		 <bound method HTTPResponse.fileno of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

flush 		 <bound method HTTPResponse.flush of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

fp 		 None 

getcode 		 <bound method HTTPResponse.getcode of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

getheader 		 <bound method HTTPResponse.getheader of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

getheaders 		 <bound method HTTPResponse.getheaders of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

geturl 		 <bound method HTTPResponse.geturl of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

headers 		 Date: Wed, 30 Sep 2020 14:58:00 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 29 Sep 2020 20:09:54 GMT
ETag: "a38-5b0795dc6b2fe"
Accept-Ranges: bytes
Content-Length: 2616
Connection: close
Content-Type: text/html; charset=UTF-8

 

info 		 <bound method HTTPResponse.info of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

isatty 		 <built-in method isatty of HTTPResponse object at 0x7f9764df25b0> 

isclosed 		 <bound method HTTPResponse.isclosed of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

length 		 0 

msg 		 OK 

peek 		 <bound method HTTPResponse.peek of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

read 		 <bound method HTTPResponse.read of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

read1 		 <bound method HTTPResponse.read1 of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

readable 		 <bound method HTTPResponse.readable of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

readinto 		 <bound method HTTPResponse.readinto of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

readinto1 		 <built-in method readinto1 of HTTPResponse object at 0x7f9764df25b0> 

readline 		 <bound method HTTPResponse.readline of <http.client.HTTPResponse object at 0x7f9764df25b0>> 

readlines 		 <built-in method readlines of HTTPResponse object at 0x7f9764df25b0> 

reason 		 OK 

seek 		 <built-in method seek of HTTPResponse object at 0x7f9764df25b0> 

seekable 		 <built-in method seekable of HTTPResponse object at 0x7f9764df25b0> 

status 		 200 

tell 		 <built-in method tell of HTTPResponse object at 0x7f9764df25b0> 

truncate 		 <built-in method truncate of HTTPResponse object at 0x7f9764df25b0> 

url 		 https://zoo.cs.yale.edu/classes/cs200/index.html 

version 		 11 

will_close 		 True 

writable 		 <built-in method writable of HTTPResponse object at 0x7f9764df25b0> 

write 		 <built-in method write of HTTPResponse object at 0x7f9764df25b0> 

writelines 		 <built-in method writelines of HTTPResponse object at 0x7f9764df25b0> 

In [59]:
dir(os)
Out[59]:
['CLD_CONTINUED',
 'CLD_DUMPED',
 'CLD_EXITED',
 'CLD_TRAPPED',
 'DirEntry',
 'EX_CANTCREAT',
 'EX_CONFIG',
 'EX_DATAERR',
 'EX_IOERR',
 'EX_NOHOST',
 'EX_NOINPUT',
 'EX_NOPERM',
 'EX_NOUSER',
 'EX_OK',
 'EX_OSERR',
 'EX_OSFILE',
 'EX_PROTOCOL',
 'EX_SOFTWARE',
 'EX_TEMPFAIL',
 'EX_UNAVAILABLE',
 'EX_USAGE',
 'F_LOCK',
 'F_OK',
 'F_TEST',
 'F_TLOCK',
 'F_ULOCK',
 'GRND_NONBLOCK',
 'GRND_RANDOM',
 'MFD_ALLOW_SEALING',
 'MFD_CLOEXEC',
 'MFD_HUGETLB',
 'MFD_HUGE_16GB',
 'MFD_HUGE_16MB',
 'MFD_HUGE_1GB',
 'MFD_HUGE_1MB',
 'MFD_HUGE_256MB',
 'MFD_HUGE_2GB',
 'MFD_HUGE_2MB',
 'MFD_HUGE_32MB',
 'MFD_HUGE_512KB',
 'MFD_HUGE_512MB',
 'MFD_HUGE_64KB',
 'MFD_HUGE_8MB',
 'MFD_HUGE_MASK',
 'MFD_HUGE_SHIFT',
 'MutableMapping',
 'NGROUPS_MAX',
 'O_ACCMODE',
 'O_APPEND',
 'O_ASYNC',
 'O_CLOEXEC',
 'O_CREAT',
 'O_DIRECT',
 'O_DIRECTORY',
 'O_DSYNC',
 'O_EXCL',
 'O_LARGEFILE',
 'O_NDELAY',
 'O_NOATIME',
 'O_NOCTTY',
 'O_NOFOLLOW',
 'O_NONBLOCK',
 'O_PATH',
 'O_RDONLY',
 'O_RDWR',
 'O_RSYNC',
 'O_SYNC',
 'O_TMPFILE',
 'O_TRUNC',
 'O_WRONLY',
 'POSIX_FADV_DONTNEED',
 'POSIX_FADV_NOREUSE',
 'POSIX_FADV_NORMAL',
 'POSIX_FADV_RANDOM',
 'POSIX_FADV_SEQUENTIAL',
 'POSIX_FADV_WILLNEED',
 'POSIX_SPAWN_CLOSE',
 'POSIX_SPAWN_DUP2',
 'POSIX_SPAWN_OPEN',
 'PRIO_PGRP',
 'PRIO_PROCESS',
 'PRIO_USER',
 'P_ALL',
 'P_NOWAIT',
 'P_NOWAITO',
 'P_PGID',
 'P_PID',
 'P_WAIT',
 'PathLike',
 'RTLD_DEEPBIND',
 'RTLD_GLOBAL',
 'RTLD_LAZY',
 'RTLD_LOCAL',
 'RTLD_NODELETE',
 'RTLD_NOLOAD',
 'RTLD_NOW',
 'RWF_DSYNC',
 'RWF_HIPRI',
 'RWF_NOWAIT',
 'RWF_SYNC',
 'R_OK',
 'SCHED_BATCH',
 'SCHED_FIFO',
 'SCHED_IDLE',
 'SCHED_OTHER',
 'SCHED_RESET_ON_FORK',
 'SCHED_RR',
 'SEEK_CUR',
 'SEEK_DATA',
 'SEEK_END',
 'SEEK_HOLE',
 'SEEK_SET',
 'ST_APPEND',
 'ST_MANDLOCK',
 'ST_NOATIME',
 'ST_NODEV',
 'ST_NODIRATIME',
 'ST_NOEXEC',
 'ST_NOSUID',
 'ST_RDONLY',
 'ST_RELATIME',
 'ST_SYNCHRONOUS',
 'ST_WRITE',
 'TMP_MAX',
 'WCONTINUED',
 'WCOREDUMP',
 'WEXITED',
 'WEXITSTATUS',
 'WIFCONTINUED',
 'WIFEXITED',
 'WIFSIGNALED',
 'WIFSTOPPED',
 'WNOHANG',
 'WNOWAIT',
 'WSTOPPED',
 'WSTOPSIG',
 'WTERMSIG',
 'WUNTRACED',
 'W_OK',
 'XATTR_CREATE',
 'XATTR_REPLACE',
 'XATTR_SIZE_MAX',
 'X_OK',
 '_Environ',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_check_methods',
 '_execvpe',
 '_exists',
 '_exit',
 '_fspath',
 '_fwalk',
 '_get_exports_list',
 '_putenv',
 '_spawnvef',
 '_unsetenv',
 '_wrap_close',
 'abc',
 'abort',
 'access',
 'altsep',
 'chdir',
 'chmod',
 'chown',
 'chroot',
 'close',
 'closerange',
 'confstr',
 'confstr_names',
 'copy_file_range',
 'cpu_count',
 'ctermid',
 'curdir',
 'defpath',
 'device_encoding',
 'devnull',
 'dup',
 'dup2',
 'environ',
 'environb',
 'error',
 'execl',
 'execle',
 'execlp',
 'execlpe',
 'execv',
 'execve',
 'execvp',
 'execvpe',
 'extsep',
 'fchdir',
 'fchmod',
 'fchown',
 'fdatasync',
 'fdopen',
 'fork',
 'forkpty',
 'fpathconf',
 'fsdecode',
 'fsencode',
 'fspath',
 'fstat',
 'fstatvfs',
 'fsync',
 'ftruncate',
 'fwalk',
 'get_blocking',
 'get_exec_path',
 'get_inheritable',
 'get_terminal_size',
 'getcwd',
 'getcwdb',
 'getegid',
 'getenv',
 'getenvb',
 'geteuid',
 'getgid',
 'getgrouplist',
 'getgroups',
 'getloadavg',
 'getlogin',
 'getpgid',
 'getpgrp',
 'getpid',
 'getppid',
 'getpriority',
 'getrandom',
 'getresgid',
 'getresuid',
 'getsid',
 'getuid',
 'getxattr',
 'initgroups',
 'isatty',
 'kill',
 'killpg',
 'lchown',
 'linesep',
 'link',
 'listdir',
 'listxattr',
 'lockf',
 'lseek',
 'lstat',
 'major',
 'makedev',
 'makedirs',
 'memfd_create',
 'minor',
 'mkdir',
 'mkfifo',
 'mknod',
 'name',
 'nice',
 'open',
 'openpty',
 'pardir',
 'path',
 'pathconf',
 'pathconf_names',
 'pathsep',
 'pipe',
 'pipe2',
 'popen',
 'posix_fadvise',
 'posix_fallocate',
 'posix_spawn',
 'posix_spawnp',
 'pread',
 'preadv',
 'putenv',
 'pwrite',
 'pwritev',
 'read',
 'readlink',
 'readv',
 'register_at_fork',
 'remove',
 'removedirs',
 'removexattr',
 'rename',
 'renames',
 'replace',
 'rmdir',
 'scandir',
 'sched_get_priority_max',
 'sched_get_priority_min',
 'sched_getaffinity',
 'sched_getparam',
 'sched_getscheduler',
 'sched_param',
 'sched_rr_get_interval',
 'sched_setaffinity',
 'sched_setparam',
 'sched_setscheduler',
 'sched_yield',
 'sendfile',
 'sep',
 'set_blocking',
 'set_inheritable',
 'setegid',
 'seteuid',
 'setgid',
 'setgroups',
 'setpgid',
 'setpgrp',
 'setpriority',
 'setregid',
 'setresgid',
 'setresuid',
 'setreuid',
 'setsid',
 'setuid',
 'setxattr',
 'spawnl',
 'spawnle',
 'spawnlp',
 'spawnlpe',
 'spawnv',
 'spawnve',
 'spawnvp',
 'spawnvpe',
 'st',
 'stat',
 'stat_result',
 'statvfs',
 'statvfs_result',
 'strerror',
 'supports_bytes_environ',
 'supports_dir_fd',
 'supports_effective_ids',
 'supports_fd',
 'supports_follow_symlinks',
 'symlink',
 'sync',
 'sys',
 'sysconf',
 'sysconf_names',
 'system',
 'tcgetpgrp',
 'tcsetpgrp',
 'terminal_size',
 'times',
 'times_result',
 'truncate',
 'ttyname',
 'umask',
 'uname',
 'uname_result',
 'unlink',
 'unsetenv',
 'urandom',
 'utime',
 'wait',
 'wait3',
 'wait4',
 'waitid',
 'waitid_result',
 'waitpid',
 'walk',
 'write',
 'writev']
In [60]:
os.__doc__
Out[60]:
"OS routines for NT or Posix depending on what system we're on.\n\nThis exports:\n  - all functions from posix or nt, e.g. unlink, stat, etc.\n  - os.path is either posixpath or ntpath\n  - os.name is either 'posix' or 'nt'\n  - os.curdir is a string representing the current directory (always '.')\n  - os.pardir is a string representing the parent directory (always '..')\n  - os.sep is the (or a most common) pathname separator ('/' or '\\\\')\n  - os.extsep is the extension separator (always '.')\n  - os.altsep is the alternate pathname separator (None or '/')\n  - os.pathsep is the component separator used in $PATH etc\n  - os.linesep is the line separator in text files ('\\r' or '\\n' or '\\r\\n')\n  - os.defpath is the default search path for executables\n  - os.devnull is the file path of the null device ('/dev/null', etc.)\n\nPrograms that import and use 'os' stand a better chance of being\nportable between different platforms.  Of course, they must then\nonly use functions that are defined by all platforms (e.g., unlink\nand opendir), and leave all pathname manipulation to os.path\n(e.g., split and join).\n"

End of Utilities notebook.

In [61]:
os.__name__
Out[61]:
'os'
In [62]:
def f():
   return 9 
In [63]:
f()
Out[63]:
9
In [64]:
dir(f)
Out[64]:
['__annotations__',
 '__call__',
 '__class__',
 '__closure__',
 '__code__',
 '__defaults__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__get__',
 '__getattribute__',
 '__globals__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__kwdefaults__',
 '__le__',
 '__lt__',
 '__module__',
 '__name__',
 '__ne__',
 '__new__',
 '__qualname__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__']
In [ ]: