1 / 10

Selected Python Utilities

Selected Python Utilities. The sys Module. There are a very large number utilities available in various Python modules We will examine a few that I have found to be quite useful We first check out two that are part of the sys module

leo-harris
Download Presentation

Selected Python Utilities

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Selected Python Utilities

  2. The sys Module • There are a very large number utilities available in various Python modules • We will examine a few that I have found to be quite useful • We first check out two that are part of the sys module • To use these functions, you must import the sys module into your program • sys.exit()Causes the Python program to stop executing, returns 0 unless you provide a value as an argument • Unix system interpret return value 0 as normal termination, 1 as a runtime error and 2 as a command-line error. • sys.argvPerhaps the most important item in the module. It is used to access command-line arguments. • sys.argv[0] always contains the name of the program being executed • The command line arguments are stored in sys.argv[1:]

  3. sys.argv • Example • # file showargs.pyimport sysfor arg in sys.argv: print(arg)print() • Here is what happens when we run this program with various command line arguments: • % python showargs.py a 1 • % python showargs.py J.D.fink • % python showargs.py "J.D. fink" For more discussion of this topic, see the bottom of page 213 of the text showargs.pya1 showargs.pyJ.D.fink showargs.pyJ.D. fink

  4. The os Module • The os module contains a few environmental values and operations similar to those in sys. • Environment: • os.sepPerhaps the most important item in the module. It is used to access command-line arguments. • os.environalways contains the name of the program being executed • os.getenv(varname[,defaultvalue])always contains the name of the program being executed

  5. The os Module • The os module contains a few environmental values and operations similar to those in sys. • However, most of what is provided is more related to file and directory manipulation. Environment • os.sepThe string used to separate path components: '/' on Unix-based systems (including MacOS), '\\' on Windows. • os.environA dictionary of environment variables (keys) and their values Changes to the entries will only be visible during Python's execution • os.getenv(varname[,defaultvalue])Returns the value of environment variable varname. If there is no such variable, returns None, or defaultvalue if provided. Why two \s?

  6. The os Module Files • os.getcwd()returns the current working directory • os.chdir(path) # last component of path must be a directoryChanges the current working directory to path. • os.mkdir(path)Creates the directory given bypath. • os.makedirs(path)Creates all the directory alongpath. • os.rmdir(path) # last component of path must be a directoryRemoves the directory given by path, which must be empty. • os.removedirs(path)# last component of path must be a directoryRemoves all directories alongpath, all of which must be empty

  7. The os Module Directory Content • os.listdir(path)Returns a list of the files in the directory given by path. The argument is not optional; and the order of the list is arbitrary.

  8. The os.pathSubmodule of os Path Components • os.path.abspath(path)Returns the full absolute version of path. Which starts, of course, at the root directory of the volume. • os.path.split(pathReturns a tuple (head,tail), where tail it the last component of path, and head is the part of the path leading up to tail. Equivalent totuple(path.rsplit(os.sep),1) • os.dirname(path)Returns the first component of os.path.split(path). • os.basename(path)Returns the first component of os.path.split(path). • os.splitext(path)If path contains '.', returns tuple(path.rsplit('.')) Example: os.splitext('\\Users\\rtindell\\dumb.py') returns ('\\Users\\rtindell\\dumb','py')

  9. The os.pathSubmodule of os Path Information • os.path.exists(path)Returns True if there is a file or directory at path. • os.path.isfile(pathReturns True if path is the name of a file. • os.path.isdir(path)Returns True if path is the name of a directory. • os.path.getsize(path)Returns the size in bytes of the file at path.

  10. Other Utilities and Modules • You should also check out the following. • The shutil module – page 234 or Google (safer!) • The string module – page 240 or Google (safer!) • The iomodule – page 242 or Google (safer!)

More Related