The Python sys module is detailed

Source: Internet
Author: User
Tags exit in stdin

SYS module features many, we introduce some of the more practical features, I believe you will like, and I went into the Python module it!

A list of common functions of the SYS module
    • sys.argv : Implements passing parameters to the program from outside the program.

    • sys.exit ([arg]) : Exit in the middle of the program, arg=0 for normal exit.

    • sys.stdout.write (' please: '): standard output, example of a progress bar
    • Sys.getrecursionlimit (): Get the maximum number of recursive layers
    • Sys.setrecursionlimit (1200): Set maximum number of recursive layers
    • sys.getdefaultencoding () : Gets the system current encoding, which is generally ASCII by default.

    • sys.setdefaultencoding () : Set system default encoding, do not see this method when executing dir (SYS), do not pass in interpreter, can execute reload (SYS) First , the system default encoding is set to UTF8 when setdefaultencoding (' UTF8 ') is executed. (See Set system default encoding)

    • sys.getfilesystemencoding () : Get file system using encoding, Windows returns ' MBCS ', Mac returns ' Utf-8 '.

    • Sys.path : Gets a collection of strings for the specified module's search path, which can be placed in a given path, and can be found correctly when import is in the program.

    • sys.platform : Gets the current system platform.

    • sys.stdin,sys.stdout,sys.stderr : stdin, stdout, and stderr variables contain stream objects that correspond to standard I/O streams. If you need more control over the output, and print does not meet your requirements, they are what you need. You can also replace them, so you can redirect output and input to other devices, or handle them in a non-standard way

sys.argv

Function: Pass parameters externally to the program
Example:sys.py

#!/usr/bin/env pythonimport sysprint sys.argv[0]print sys.argv[1]     

Run:

# python sys.py argv1sys.pyargv1 

Try it yourself, understand the parameters corresponding to the relationship

Sys.exit (N)

Function: Executes to the end of the main program, the interpreter automatically exits, but if you need to exit the program halfway, you can call the Sys.exit function, with an optional integer parameter returned to the program that called it, indicating that you can capture the call to Sys.exit in the main program. (0 is normal exit, others are abnormal)

Example:exit.py

#!/usr/bin/env pythonimport sysdefprint value sys.exit (0)"Hello"try : Sys.exit (1)except systemexit,value:exitfunc (value)"come?"    

Run:

# python exit.pyhello1 

Sys.path

Function: Gets a collection of strings for the specified module search path, which can be placed in a given path, and can be found correctly when import is in the program.

Example:

>>>ImportSys>>>sys.path[‘‘,‘/usr/lib/python2.7‘,‘/usr/lib/python2.7/plat-x86_64-linux-gnu‘,‘/usr/lib/python2.7/lib-tk‘,‘/usr/lib/python2.7/lib-old' , '/usr/lib/python2.7/lib-dynload ', '/usr/local/lib/python2.7/dist-packages', ' /usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/pilcompat', ' /usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ Ubuntu-sso-client']                

sys.path.append("自定义模块路径")

Sys.modules

Function: sys.modules is a global dictionary that is loaded in memory when Python is started. Whenever a programmer imports a new module, sys.modules the module is automatically recorded. When the module is re-imported the second time, Python will look directly into the dictionary, speeding up the program's speed. It has all the methods that the dictionary has.

Example:modules.py

#!/usr/bin/env pythonimport sysprint sys.modules.keys ()print sys.modules.values () print sys.modules["os"]          

Run:

Python modules.py['copy_reg'sre_compile'_sre'encodings'site' __builtin__',......            

Sys.stdin\stdout\stderr

Features: stdin, stdout, and stderr variables contain stream objects that correspond to standard I/O streams. If you need more control over the output, and print does not meet your requirements, they are what you need. You can also replace them, so you can redirect output and input to other devices, or handle them in a non-standard way

Reprint: http://www.cnblogs.com/cherishry/p/5725184.html

The Python sys module is detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.