{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## CS 200: Utilities in Python\n", "\n", "This notebook mirrors the Google Python Course: Utilities\n", "\n", "\n", "\n", "### File System -- os, os.path, shutil\n", "\n", "The *os* and *os.path* modules include many functions to interact with the file system. The *shutil* module can copy files.\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['.swirl',\n", " '__pycache__',\n", " 'nn',\n", " '.old2018',\n", " 'sh',\n", " 'sklearn',\n", " '.old2017',\n", " 'f0201.py',\n", " 'f0831.py',\n", " 'cs200.ipynb',\n", " 'notebook.py',\n", " '0201.html',\n", " '0203.html',\n", " 'psource.py',\n", " 'collatz.py',\n", " 'python.html',\n", " '0201nb.html',\n", " 'google-python-exercises',\n", " '0201.script',\n", " '0201nb.ipynb',\n", " '0208.html',\n", " '0203.script',\n", " '0210.html',\n", " '0215.html',\n", " 'f0909.py',\n", " 'Introduction.ipynb',\n", " 'Strings.ipynb',\n", " '0208.script',\n", " 'Introduction.html',\n", " 'cs200.html',\n", " 'Strings.html',\n", " 'testfile',\n", " 'Lists.html',\n", " '0210.script',\n", " 'Lists.ipynb',\n", " 'Sorting.html',\n", " 'Sorting.ipynb',\n", " 'newdir',\n", " 'recursion.py',\n", " 'fib.py',\n", " 'DictFiles.ipynb',\n", " 'RegExp.ipynb',\n", " 'Utilities.ipynb',\n", " '0217.html',\n", " '0224.html',\n", " 'linux.words',\n", " 'DictFiles.html',\n", " 'puzzle.py',\n", " 'Recursion.html',\n", " 'Recursion.ipynb',\n", " 'listcomp.py',\n", " '0217.script',\n", " '0301.html',\n", " 'gullible.png',\n", " 'Listcomp.html',\n", " 'knapsack.py',\n", " 'Listcomp.ipynb',\n", " '0224.script',\n", " 'retest.py',\n", " 'RegExp.html',\n", " 'mt.py',\n", " 'is-this-going.jpg',\n", " 'Utilities.html',\n", " 'hw3a.pyc',\n", " 'Hw3.html',\n", " 'Hw3.ipynb',\n", " '.old2016',\n", " '.old2019',\n", " 'DeepRecursion.html',\n", " 'testfile2',\n", " 'testfile3',\n", " '.old2020',\n", " 'DeepRecursion.ipynb',\n", " '.old2020f',\n", " 'Oop.html',\n", " 'decorators.py',\n", " 'ls.output',\n", " 'testfile11',\n", " '.ipynb_checkpoints',\n", " '.#0301.script',\n", " 'Oop.ipynb',\n", " 'exceptions.html',\n", " 'exceptions.py',\n", " 'iterators.py',\n", " '0301.html~',\n", " 'Exceptions.html',\n", " 'Exceptions.ipynb',\n", " 'Decorators.html',\n", " '0301.script',\n", " 'Decorators.ipynb',\n", " '#0301.script#',\n", " 'fire',\n", " 'calc.py',\n", " 'practicemidterm1ans.pdf',\n", " 'oop.py',\n", " 'cryptopals',\n", " 'practicemidterm1.pdf',\n", " 'midterm1review.pdf',\n", " 'mt.script',\n", " 'mt.script.answer',\n", " 'hamlet.py',\n", " 'stack.py',\n", " 'trees.py',\n", " 'graph.py',\n", " 'Iterators.html',\n", " 'Iterators.ipynb',\n", " 'DataStructures.ipynb',\n", " 'Matplotlib.html',\n", " 'Matplotlib.ipynb',\n", " 'f0217.py',\n", " 'DataStructures.html',\n", " 'hw4a.pyc',\n", " ',',\n", " 'pvm.py',\n", " 'script',\n", " 'script2',\n", " 'bytecode.py',\n", " 'PVM.ipynb',\n", " 'PVM.html',\n", " 'hw5a.pyc',\n", " 'practicemidterm2ans.pdf',\n", " 'Analysis.ipynb',\n", " 'practicemidterm2.pdf',\n", " 'Analysis.html',\n", " 'sql.py',\n", " 'SQL.ipynb',\n", " 'mydb.db',\n", " 'SQL.html',\n", " 'SkiennaSorting.pdf',\n", " 'PVM(1).html',\n", " 'file1',\n", " 'file2',\n", " 'oop.html',\n", " 'crypto.html',\n", " 'Crypto.html',\n", " 'Crypto.ipynb',\n", " 'test.png',\n", " 'ML.ipynb',\n", " 'script3',\n", " 'ML.html',\n", " 'prep_terrain_data.py',\n", " 'class_vis.py']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir(\".\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "p = os.path.abspath('.')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/httpd/html/zoo/classes/cs200/lectures'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "f = os.path.join(p,'retest.py')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/httpd/html/zoo/classes/cs200/lectures/retest.py'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/httpd/html/zoo/classes/cs200/lectures'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.dirname(f)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'retest.py'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.basename(f)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.exists(f)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "newdir = os.path.join(p,\"newdir\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.exists(newdir)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "FileExistsError", "evalue": "[Errno 17] File exists: '/home/httpd/html/zoo/classes/cs200/lectures/newdir'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileExistsError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmkdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnewdir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mFileExistsError\u001b[0m: [Errno 17] File exists: '/home/httpd/html/zoo/classes/cs200/lectures/newdir'" ] } ], "source": [ "os.mkdir(newdir)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.exists(newdir)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "newpath = os.path.join(newdir, 'a/b/c')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/httpd/html/zoo/classes/cs200/lectures/newdir/a/b/c'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "newpath" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.exists(newpath)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "FileExistsError", "evalue": "[Errno 17] File exists: '/home/httpd/html/zoo/classes/cs200/lectures/newdir/a/b/c'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileExistsError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmakedirs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnewpath\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m## create all needed directories\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/usr/lib64/python3.8/os.py\u001b[0m in \u001b[0;36mmakedirs\u001b[0;34m(name, mode, exist_ok)\u001b[0m\n\u001b[1;32m 221\u001b[0m \u001b[0;32mreturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 222\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 223\u001b[0;31m \u001b[0mmkdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 224\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 225\u001b[0m \u001b[0;31m# Cannot rely on checking for EEXIST, since the operating system\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mFileExistsError\u001b[0m: [Errno 17] File exists: '/home/httpd/html/zoo/classes/cs200/lectures/newdir/a/b/c'" ] } ], "source": [ "os.makedirs(newpath) ## create all needed directories" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.exists(newpath)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "import shutil" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/httpd/html/zoo/classes/cs200/lectures/newdir/retest.py'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shutil.copy('./retest.py', newdir)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'retest.py']" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir(newdir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Running External Processes -- commands subprocess

\n", "\n", "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.\n", "\n", "
    \n", "
  • subprocess module docs\n", "\n", "
  • (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.\n", "\n", "
  • output = subprocess.getoutput(cmd) -- as above, but without the status int.\n", "\n", "
  • There is no subprocess.getstatus() but we can define a similar function.\n", "\n", "
  • If you want more control over the running of the sub-process, see the \"popen2\" module (http://docs.python.org/lib/module-popen2.html)\n", "\n", "
  • 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." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "import subprocess" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "(status, output) = subprocess.getstatusoutput(\"date\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "status" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mon 01 Mar 2021 02:46:52 PM EST'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "output" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mon 01 Mar 2021 02:48:24 PM EST'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "subprocess.getoutput(\"date\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "module 'subprocess' has no attribute 'getstatus'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msubprocess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetstatus\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"date\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: module 'subprocess' has no attribute 'getstatus'" ] } ], "source": [ "subprocess.getstatus(\"date\")" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "def mygetstatus(cmd):\n", " (status, output) = subprocess.getstatusoutput(cmd)\n", " return status" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mygetstatus('date')" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "127" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mygetstatus('xsxsxs')" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mygetstatus('ls /djdjd')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exceptions\n", "\n", "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]])" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'joe' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mjoe\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'joe' is not defined" ] } ], "source": [ "joe" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'somefile'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'somefile'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'somefile'" ] } ], "source": [ "fd = open('somefile','r')" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'fd' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfd\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'fd' is not defined" ] } ], "source": [ "fd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "somefile not found. Sorry.\n", "We keep on going.\n" ] } ], "source": [ "try:\n", " file = open(\"somefile\",'r')\n", " text = file.read()\n", " file.close()\n", "except FileNotFoundError:\n", " print (\"somefile not found. Sorry.\")\n", "print (\"We keep on going.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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)\".\n", "\n", "A later notebook will explore exceptions in greater detail." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### HTTP -- urllib and urlparse\n", "\n", "#### Video:\n", "\n", "See urllib video from Socratica.\n", "\n", "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.\n", "\n", "
      \n", "
    • urllib module docs\n", "\n", "
    • ufile = urllib.request.urlopen(url) -- returns a file like object for that url\n", "\n", "
    • text = ufile.read() -- can read from it, like a file (readlines(), for loops, etc. also work)\n", "
    • \n", "info = ufile.info() -- the meta info for that request. info.gettype() is the mime time, e.g. 'text/html'\n", "\n", "
    • baseurl = ufile.geturl() -- gets the \"base\" url for the request, which may be different from the original because of redirects\n", "\n", "
    • urllib.request.urlretrieve(url, filename) -- downloads the url data to the given file path\n", "\n", "
    • 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.\n", "
    " ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "import urllib" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "cs200url = 'https://zoo.cs.yale.edu/classes/cs200/index.html'" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "ufile = urllib.request.urlopen(cs200url)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "http.client.HTTPResponse" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(ufile)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "text = ufile.read()" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bytes" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(text)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\n\\nCPSC 200 - Introduc'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text[0:40]" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2749" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(text)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<http.client.HTTPMessage at 0x7f4244267250>" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ufile.info()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "http.client.HTTPMessage" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(ufile.info())" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "baseurl = ufile.geturl()" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://zoo.cs.yale.edu/classes/cs200/index.html'" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "baseurl" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The for loop and iterate through the url page, just as it can iterate through a file, line by line." ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<HTML>\n", "<HEAD>\n", "<TITLE>CPSC 200 - Introduction to Information Systems\n", " \n", "\n", "\n", "
    \n", "

    CPSC 200 - Introduction to Information Systems

    \n", "

    SPRING 2021

    \n" ] } ], "source": [ "count = 0\n", "for line in urllib.request.urlopen(cs200url):\n", " count += 1\n", " if count < 10:\n", " print (line.decode('utf-8'), end='')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we try the urlretrieve() method." ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('copyofcs200url', )" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "urllib.request.urlretrieve(cs200url, \"copyofcs200url\")" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "CPSC 200 - Introduction to Information Systems\n", " \n", "\n", "\n", "
    \n", "

    CPSC 200 - Introduction to Information Systems

    \n", "

    SPRING 2021

    \n", "
    \n" ] } ], "source": [ "with open('copyofcs200url','r') as f:\n", " count = 0\n", " for line in f:\n", " count += 1\n", " if count > 10:\n", " pass\n", " else:\n", " print (line, end='')" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://zoo.cs.yale.edu/classes/cs200/index.html'" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "urllib.parse.urljoin(baseurl, cs200url)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can explore these objects uring the dir() command and related functions. These techniques are generally useful, beyond the urllib module." ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['AbstractBasicAuthHandler',\n", " 'AbstractDigestAuthHandler',\n", " 'AbstractHTTPHandler',\n", " 'BaseHandler',\n", " 'CacheFTPHandler',\n", " 'ContentTooShortError',\n", " 'DataHandler',\n", " 'FTPHandler',\n", " 'FancyURLopener',\n", " 'FileHandler',\n", " 'HTTPBasicAuthHandler',\n", " 'HTTPCookieProcessor',\n", " 'HTTPDefaultErrorHandler',\n", " 'HTTPDigestAuthHandler',\n", " 'HTTPError',\n", " 'HTTPErrorProcessor',\n", " 'HTTPHandler',\n", " 'HTTPPasswordMgr',\n", " 'HTTPPasswordMgrWithDefaultRealm',\n", " 'HTTPPasswordMgrWithPriorAuth',\n", " 'HTTPRedirectHandler',\n", " 'HTTPSHandler',\n", " 'MAXFTPCACHE',\n", " 'OpenerDirector',\n", " 'ProxyBasicAuthHandler',\n", " 'ProxyDigestAuthHandler',\n", " 'ProxyHandler',\n", " 'Request',\n", " 'URLError',\n", " 'URLopener',\n", " 'UnknownHandler',\n", " '__all__',\n", " '__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " '__version__',\n", " '_cut_port_re',\n", " '_ftperrors',\n", " '_have_ssl',\n", " '_localhost',\n", " '_noheaders',\n", " '_opener',\n", " '_parse_proxy',\n", " '_proxy_bypass_macosx_sysconf',\n", " '_randombytes',\n", " '_safe_gethostbyname',\n", " '_splitattr',\n", " '_splithost',\n", " '_splitpasswd',\n", " '_splitport',\n", " '_splitquery',\n", " '_splittag',\n", " '_splittype',\n", " '_splituser',\n", " '_splitvalue',\n", " '_thishost',\n", " '_to_bytes',\n", " '_url_tempfiles',\n", " 'addclosehook',\n", " 'addinfourl',\n", " 'base64',\n", " 'bisect',\n", " 'build_opener',\n", " 'contextlib',\n", " 'email',\n", " 'ftpcache',\n", " 'ftperrors',\n", " 'ftpwrapper',\n", " 'getproxies',\n", " 'getproxies_environment',\n", " 'hashlib',\n", " 'http',\n", " 'install_opener',\n", " 'io',\n", " 'localhost',\n", " 'noheaders',\n", " 'os',\n", " 'parse_http_list',\n", " 'parse_keqv_list',\n", " 'pathname2url',\n", " 'posixpath',\n", " 'proxy_bypass',\n", " 'proxy_bypass_environment',\n", " 'quote',\n", " 're',\n", " 'request_host',\n", " 'socket',\n", " 'ssl',\n", " 'string',\n", " 'sys',\n", " 'tempfile',\n", " 'thishost',\n", " 'time',\n", " 'unquote',\n", " 'unquote_to_bytes',\n", " 'unwrap',\n", " 'url2pathname',\n", " 'urlcleanup',\n", " 'urljoin',\n", " 'urlopen',\n", " 'urlparse',\n", " 'urlretrieve',\n", " 'urlsplit',\n", " 'urlunparse',\n", " 'warnings']" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(urllib.request)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ufile.length" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "200" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ufile.status" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__abstractmethods__',\n", " '__class__',\n", " '__del__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__enter__',\n", " '__eq__',\n", " '__exit__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__iter__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__ne__',\n", " '__new__',\n", " '__next__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " '_abc_impl',\n", " '_checkClosed',\n", " '_checkReadable',\n", " '_checkSeekable',\n", " '_checkWritable',\n", " '_check_close',\n", " '_close_conn',\n", " '_get_chunk_left',\n", " '_method',\n", " '_peek_chunked',\n", " '_read1_chunked',\n", " '_read_and_discard_trailer',\n", " '_read_next_chunk_size',\n", " '_read_status',\n", " '_readall_chunked',\n", " '_readinto_chunked',\n", " '_safe_read',\n", " '_safe_readinto',\n", " 'begin',\n", " 'chunk_left',\n", " 'chunked',\n", " 'close',\n", " 'closed',\n", " 'code',\n", " 'debuglevel',\n", " 'detach',\n", " 'fileno',\n", " 'flush',\n", " 'fp',\n", " 'getcode',\n", " 'getheader',\n", " 'getheaders',\n", " 'geturl',\n", " 'headers',\n", " 'info',\n", " 'isatty',\n", " 'isclosed',\n", " 'length',\n", " 'msg',\n", " 'peek',\n", " 'read',\n", " 'read1',\n", " 'readable',\n", " 'readinto',\n", " 'readinto1',\n", " 'readline',\n", " 'readlines',\n", " 'reason',\n", " 'seek',\n", " 'seekable',\n", " 'status',\n", " 'tell',\n", " 'truncate',\n", " 'url',\n", " 'version',\n", " 'will_close',\n", " 'writable',\n", " 'write',\n", " 'writelines']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(ufile)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://zoo.cs.yale.edu/classes/cs200/index.html'" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ufile.url" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Neat trick for analyzing a module's properties and methods." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "begin \t\t > \n", "\n", "chunk_left \t\t UNKNOWN \n", "\n", "chunked \t\t False \n", "\n", "close \t\t > \n", "\n", "closed \t\t False \n", "\n", "code \t\t 200 \n", "\n", "debuglevel \t\t 0 \n", "\n", "detach \t\t \n", "\n", "fileno \t\t > \n", "\n", "flush \t\t > \n", "\n", "fp \t\t None \n", "\n", "getcode \t\t > \n", "\n", "getheader \t\t > \n", "\n", "getheaders \t\t > \n", "\n", "geturl \t\t > \n", "\n", "headers \t\t Date: Wed, 30 Sep 2020 14:58:00 GMT\n", "Server: Apache/2.4.6 (Red Hat Enterprise Linux)\n", "Last-Modified: Tue, 29 Sep 2020 20:09:54 GMT\n", "ETag: \"a38-5b0795dc6b2fe\"\n", "Accept-Ranges: bytes\n", "Content-Length: 2616\n", "Connection: close\n", "Content-Type: text/html; charset=UTF-8\n", "\n", " \n", "\n", "info \t\t > \n", "\n", "isatty \t\t \n", "\n", "isclosed \t\t > \n", "\n", "length \t\t 0 \n", "\n", "msg \t\t OK \n", "\n", "peek \t\t > \n", "\n", "read \t\t > \n", "\n", "read1 \t\t > \n", "\n", "readable \t\t > \n", "\n", "readinto \t\t > \n", "\n", "readinto1 \t\t \n", "\n", "readline \t\t > \n", "\n", "readlines \t\t \n", "\n", "reason \t\t OK \n", "\n", "seek \t\t \n", "\n", "seekable \t\t \n", "\n", "status \t\t 200 \n", "\n", "tell \t\t \n", "\n", "truncate \t\t \n", "\n", "url \t\t https://zoo.cs.yale.edu/classes/cs200/index.html \n", "\n", "version \t\t 11 \n", "\n", "will_close \t\t True \n", "\n", "writable \t\t \n", "\n", "write \t\t \n", "\n", "writelines \t\t \n", "\n" ] } ], "source": [ "for x in dir(ufile):\n", " if x.startswith('_'):\n", " pass\n", " else:\n", " print (x, '\\t\\t', getattr(ufile, x), '\\n')" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['CLD_CONTINUED',\n", " 'CLD_DUMPED',\n", " 'CLD_EXITED',\n", " 'CLD_TRAPPED',\n", " 'DirEntry',\n", " 'EX_CANTCREAT',\n", " 'EX_CONFIG',\n", " 'EX_DATAERR',\n", " 'EX_IOERR',\n", " 'EX_NOHOST',\n", " 'EX_NOINPUT',\n", " 'EX_NOPERM',\n", " 'EX_NOUSER',\n", " 'EX_OK',\n", " 'EX_OSERR',\n", " 'EX_OSFILE',\n", " 'EX_PROTOCOL',\n", " 'EX_SOFTWARE',\n", " 'EX_TEMPFAIL',\n", " 'EX_UNAVAILABLE',\n", " 'EX_USAGE',\n", " 'F_LOCK',\n", " 'F_OK',\n", " 'F_TEST',\n", " 'F_TLOCK',\n", " 'F_ULOCK',\n", " 'GRND_NONBLOCK',\n", " 'GRND_RANDOM',\n", " 'MFD_ALLOW_SEALING',\n", " 'MFD_CLOEXEC',\n", " 'MFD_HUGETLB',\n", " 'MFD_HUGE_16GB',\n", " 'MFD_HUGE_16MB',\n", " 'MFD_HUGE_1GB',\n", " 'MFD_HUGE_1MB',\n", " 'MFD_HUGE_256MB',\n", " 'MFD_HUGE_2GB',\n", " 'MFD_HUGE_2MB',\n", " 'MFD_HUGE_32MB',\n", " 'MFD_HUGE_512KB',\n", " 'MFD_HUGE_512MB',\n", " 'MFD_HUGE_64KB',\n", " 'MFD_HUGE_8MB',\n", " 'MFD_HUGE_MASK',\n", " 'MFD_HUGE_SHIFT',\n", " 'MutableMapping',\n", " 'NGROUPS_MAX',\n", " 'O_ACCMODE',\n", " 'O_APPEND',\n", " 'O_ASYNC',\n", " 'O_CLOEXEC',\n", " 'O_CREAT',\n", " 'O_DIRECT',\n", " 'O_DIRECTORY',\n", " 'O_DSYNC',\n", " 'O_EXCL',\n", " 'O_LARGEFILE',\n", " 'O_NDELAY',\n", " 'O_NOATIME',\n", " 'O_NOCTTY',\n", " 'O_NOFOLLOW',\n", " 'O_NONBLOCK',\n", " 'O_PATH',\n", " 'O_RDONLY',\n", " 'O_RDWR',\n", " 'O_RSYNC',\n", " 'O_SYNC',\n", " 'O_TMPFILE',\n", " 'O_TRUNC',\n", " 'O_WRONLY',\n", " 'POSIX_FADV_DONTNEED',\n", " 'POSIX_FADV_NOREUSE',\n", " 'POSIX_FADV_NORMAL',\n", " 'POSIX_FADV_RANDOM',\n", " 'POSIX_FADV_SEQUENTIAL',\n", " 'POSIX_FADV_WILLNEED',\n", " 'POSIX_SPAWN_CLOSE',\n", " 'POSIX_SPAWN_DUP2',\n", " 'POSIX_SPAWN_OPEN',\n", " 'PRIO_PGRP',\n", " 'PRIO_PROCESS',\n", " 'PRIO_USER',\n", " 'P_ALL',\n", " 'P_NOWAIT',\n", " 'P_NOWAITO',\n", " 'P_PGID',\n", " 'P_PID',\n", " 'P_WAIT',\n", " 'PathLike',\n", " 'RTLD_DEEPBIND',\n", " 'RTLD_GLOBAL',\n", " 'RTLD_LAZY',\n", " 'RTLD_LOCAL',\n", " 'RTLD_NODELETE',\n", " 'RTLD_NOLOAD',\n", " 'RTLD_NOW',\n", " 'RWF_DSYNC',\n", " 'RWF_HIPRI',\n", " 'RWF_NOWAIT',\n", " 'RWF_SYNC',\n", " 'R_OK',\n", " 'SCHED_BATCH',\n", " 'SCHED_FIFO',\n", " 'SCHED_IDLE',\n", " 'SCHED_OTHER',\n", " 'SCHED_RESET_ON_FORK',\n", " 'SCHED_RR',\n", " 'SEEK_CUR',\n", " 'SEEK_DATA',\n", " 'SEEK_END',\n", " 'SEEK_HOLE',\n", " 'SEEK_SET',\n", " 'ST_APPEND',\n", " 'ST_MANDLOCK',\n", " 'ST_NOATIME',\n", " 'ST_NODEV',\n", " 'ST_NODIRATIME',\n", " 'ST_NOEXEC',\n", " 'ST_NOSUID',\n", " 'ST_RDONLY',\n", " 'ST_RELATIME',\n", " 'ST_SYNCHRONOUS',\n", " 'ST_WRITE',\n", " 'TMP_MAX',\n", " 'WCONTINUED',\n", " 'WCOREDUMP',\n", " 'WEXITED',\n", " 'WEXITSTATUS',\n", " 'WIFCONTINUED',\n", " 'WIFEXITED',\n", " 'WIFSIGNALED',\n", " 'WIFSTOPPED',\n", " 'WNOHANG',\n", " 'WNOWAIT',\n", " 'WSTOPPED',\n", " 'WSTOPSIG',\n", " 'WTERMSIG',\n", " 'WUNTRACED',\n", " 'W_OK',\n", " 'XATTR_CREATE',\n", " 'XATTR_REPLACE',\n", " 'XATTR_SIZE_MAX',\n", " 'X_OK',\n", " '_Environ',\n", " '__all__',\n", " '__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " '_check_methods',\n", " '_execvpe',\n", " '_exists',\n", " '_exit',\n", " '_fspath',\n", " '_fwalk',\n", " '_get_exports_list',\n", " '_putenv',\n", " '_spawnvef',\n", " '_unsetenv',\n", " '_wrap_close',\n", " 'abc',\n", " 'abort',\n", " 'access',\n", " 'altsep',\n", " 'chdir',\n", " 'chmod',\n", " 'chown',\n", " 'chroot',\n", " 'close',\n", " 'closerange',\n", " 'confstr',\n", " 'confstr_names',\n", " 'copy_file_range',\n", " 'cpu_count',\n", " 'ctermid',\n", " 'curdir',\n", " 'defpath',\n", " 'device_encoding',\n", " 'devnull',\n", " 'dup',\n", " 'dup2',\n", " 'environ',\n", " 'environb',\n", " 'error',\n", " 'execl',\n", " 'execle',\n", " 'execlp',\n", " 'execlpe',\n", " 'execv',\n", " 'execve',\n", " 'execvp',\n", " 'execvpe',\n", " 'extsep',\n", " 'fchdir',\n", " 'fchmod',\n", " 'fchown',\n", " 'fdatasync',\n", " 'fdopen',\n", " 'fork',\n", " 'forkpty',\n", " 'fpathconf',\n", " 'fsdecode',\n", " 'fsencode',\n", " 'fspath',\n", " 'fstat',\n", " 'fstatvfs',\n", " 'fsync',\n", " 'ftruncate',\n", " 'fwalk',\n", " 'get_blocking',\n", " 'get_exec_path',\n", " 'get_inheritable',\n", " 'get_terminal_size',\n", " 'getcwd',\n", " 'getcwdb',\n", " 'getegid',\n", " 'getenv',\n", " 'getenvb',\n", " 'geteuid',\n", " 'getgid',\n", " 'getgrouplist',\n", " 'getgroups',\n", " 'getloadavg',\n", " 'getlogin',\n", " 'getpgid',\n", " 'getpgrp',\n", " 'getpid',\n", " 'getppid',\n", " 'getpriority',\n", " 'getrandom',\n", " 'getresgid',\n", " 'getresuid',\n", " 'getsid',\n", " 'getuid',\n", " 'getxattr',\n", " 'initgroups',\n", " 'isatty',\n", " 'kill',\n", " 'killpg',\n", " 'lchown',\n", " 'linesep',\n", " 'link',\n", " 'listdir',\n", " 'listxattr',\n", " 'lockf',\n", " 'lseek',\n", " 'lstat',\n", " 'major',\n", " 'makedev',\n", " 'makedirs',\n", " 'memfd_create',\n", " 'minor',\n", " 'mkdir',\n", " 'mkfifo',\n", " 'mknod',\n", " 'name',\n", " 'nice',\n", " 'open',\n", " 'openpty',\n", " 'pardir',\n", " 'path',\n", " 'pathconf',\n", " 'pathconf_names',\n", " 'pathsep',\n", " 'pipe',\n", " 'pipe2',\n", " 'popen',\n", " 'posix_fadvise',\n", " 'posix_fallocate',\n", " 'posix_spawn',\n", " 'posix_spawnp',\n", " 'pread',\n", " 'preadv',\n", " 'putenv',\n", " 'pwrite',\n", " 'pwritev',\n", " 'read',\n", " 'readlink',\n", " 'readv',\n", " 'register_at_fork',\n", " 'remove',\n", " 'removedirs',\n", " 'removexattr',\n", " 'rename',\n", " 'renames',\n", " 'replace',\n", " 'rmdir',\n", " 'scandir',\n", " 'sched_get_priority_max',\n", " 'sched_get_priority_min',\n", " 'sched_getaffinity',\n", " 'sched_getparam',\n", " 'sched_getscheduler',\n", " 'sched_param',\n", " 'sched_rr_get_interval',\n", " 'sched_setaffinity',\n", " 'sched_setparam',\n", " 'sched_setscheduler',\n", " 'sched_yield',\n", " 'sendfile',\n", " 'sep',\n", " 'set_blocking',\n", " 'set_inheritable',\n", " 'setegid',\n", " 'seteuid',\n", " 'setgid',\n", " 'setgroups',\n", " 'setpgid',\n", " 'setpgrp',\n", " 'setpriority',\n", " 'setregid',\n", " 'setresgid',\n", " 'setresuid',\n", " 'setreuid',\n", " 'setsid',\n", " 'setuid',\n", " 'setxattr',\n", " 'spawnl',\n", " 'spawnle',\n", " 'spawnlp',\n", " 'spawnlpe',\n", " 'spawnv',\n", " 'spawnve',\n", " 'spawnvp',\n", " 'spawnvpe',\n", " 'st',\n", " 'stat',\n", " 'stat_result',\n", " 'statvfs',\n", " 'statvfs_result',\n", " 'strerror',\n", " 'supports_bytes_environ',\n", " 'supports_dir_fd',\n", " 'supports_effective_ids',\n", " 'supports_fd',\n", " 'supports_follow_symlinks',\n", " 'symlink',\n", " 'sync',\n", " 'sys',\n", " 'sysconf',\n", " 'sysconf_names',\n", " 'system',\n", " 'tcgetpgrp',\n", " 'tcsetpgrp',\n", " 'terminal_size',\n", " 'times',\n", " 'times_result',\n", " 'truncate',\n", " 'ttyname',\n", " 'umask',\n", " 'uname',\n", " 'uname_result',\n", " 'unlink',\n", " 'unsetenv',\n", " 'urandom',\n", " 'utime',\n", " 'wait',\n", " 'wait3',\n", " 'wait4',\n", " 'waitid',\n", " 'waitid_result',\n", " 'waitpid',\n", " 'walk',\n", " 'write',\n", " 'writev']" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(os)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"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\"" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.__doc__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "End of Utilities notebook." ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'os'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.__name__" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "def f():\n", " return 9 " ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__annotations__',\n", " '__call__',\n", " '__class__',\n", " '__closure__',\n", " '__code__',\n", " '__defaults__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__get__',\n", " '__getattribute__',\n", " '__globals__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__kwdefaults__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__name__',\n", " '__ne__',\n", " '__new__',\n", " '__qualname__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__']" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(f)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.7" } }, "nbformat": 4, "nbformat_minor": 4 }