Show
The sys Module The Working with Command-line Arguments The Example 1-66. Using the sys Module to Get Script Arguments File: sys-argv-example-1.py import sys print "script name is", sys.argv[0] if len(sys.argv) > 1: print "there are", len(sys.argv)-1, "arguments:" for arg in sys.argv[1:]: print arg else: print "there are no arguments!" If you read the script from standard input (like “ Working with Modules
The Example 1-67. Using the sys Module to Manipulate the Module Search Path File: sys-path-example-1.py import sys print "path has", len(sys.path), "members" # add the sample directory to the path sys.path.insert(0, "samples") import sample # nuke the path sys.path = [] import random # oops!
Example 1-68 demonstrates the Example 1-68. Using the sys Module to Find Built-in Modules File: sys-builtin-module-names-example-1.py import sys def dump(module): print module, "=>", if module in sys.builtin_module_names: print "<BUILTIN>" else: module = _ _import_ _(module) print module._ _file_ _ dump("os") dump("sys") dump("string") dump("strop") dump("zlib") The As you can see from Example 1-69, Python loads quite a bunch of modules before handing control over to your script. Example 1-69. Using the sys Module to Find Imported Modules File: sys-modules-example-1.py import sys print sys.modules.keys() Working with Reference Counts The Example 1-70. Using the sys Module to Find the Reference Count File: sys-getrefcount-example-1.py import sys variable = 1234 print sys.getrefcount(0) print sys.getrefcount(variable) print sys.getrefcount(None) Note that this value is always larger than the actual count, since the function itself hangs on to the object while determining the value. Checking the Host Platform Example 1-71 shows the Example 1-71. Using the sys Module to Find the Current Platform File: sys-platform-example-1.py import sys # # emulate "import os.path" (sort of)... if sys.platform == "win32": import ntpath pathmodule = ntpath elif sys.platform == "mac": import macpath pathmodule = macpath else: # assume it's a posix platform import posixpath pathmodule = posixpath print pathmodule Typical platform names are Tracing the Program The Example 1-72. Using the sys Module to Install a Profiler Function File: sys-setprofiler-example-1.py import sys def test(n): j = 0 for i in range(n): j = j + i return n def profiler(frame, event, arg): print event, frame.f_code.co_name, frame.f_lineno, "->", arg # profiler is activated on the next call, return, or exception sys.setprofile(profiler) # profile this function call test(1) # disable profiler sys.setprofile(None) # don't profile this call test(2) The The Example 1-73. Using the sys Module to Install a trace Function File: sys-settrace-example-1.py import sys def test(n): j = 0 for i in range(n): j = j + i return n def tracer(frame, event, arg): print event, frame.f_code.co_name, frame.f_lineno, "->", arg return tracer # tracer is activated on the next call, return, or exception sys.settrace(tracer) # trace this function call test(1) # disable tracing sys.settrace(None) # don't trace this call test(2) The Working with Standard Input and Output
The Example 1-74. Using the sys Module to Redirect Output File: sys-stdout-example-1.py import sys import string class Redirect: def _ _init_ _(self, stdout): self.stdout = stdout def write(self, s): self.stdout.write(string.lower(s)) # redirect standard output (including the print statement) old_stdout = sys.stdout sys.stdout = Redirect(sys.stdout) print "HEJA SVERIGE", print "FRISKT HUM\303\226R" # restore standard output sys.stdout = old_stdout print "M\303\205\303\205\303\205\303\205L!" An object that implements the (Unless it’s a C type instance, that is:
Python uses an integer attribute called Exiting the Program When you reach the end of the main
program, the interpreter is automatically terminated. If you need to exit in midflight, you can call the Example 1-75. Using the sys Module to Exit the Program File: sys-exit-example-1.py
import sys
print "hello"
sys.exit(1)
print "there"
It may not be obvious, but Example 1-76. Catching the sys.exit Call File: sys-exit-example-2.py import sys print "hello" try: sys.exit(1) except SystemExit: pass print "there" If you want to clean things up after yourself, you can install an “exit handler,” which is a function that is automatically called on the way out. This is shown in Example 1-77. Example 1-77. Catching the sys.exit Call Another Way File: sys-exitfunc-example-1.py
import sys
def exitfunc():
print "world"
sys.exitfunc = exitfunc
print "hello"
sys.exit(1)
print "there" # never printed
In Python 2.0, you can use the Get Python Standard Library now with the O’Reilly learning platform. O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers. What is the common use of Python SYS library Mcq?What is the common use of Python sys library Mcq? Explanation: The function print(dir(sys)) helps us to obtain a list of all the functions defined under the sys module. The function can be used to obtain the list of functions under any given module in Python.
What is the use of SYS module?This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available.
Do we need to install SYS in Python?The sys module comes packaged with Python, which means you do not need to download and install it separately using the PIP package manager.
|