100 likes | 222 Views
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
E N D
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:]
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
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
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?
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
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.
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')
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.
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!)