4Python standard Library Series SYS module

Source: Internet
Author: User
Tags python script

The Sys module of the Python standard library series


This module provides access to some variables used or maintained by the interpreter and to functions that interact Strongl Y with the interpreter. It's always available.

sysThe module is used to provide an interpreter-related action

Module Method Explanatory notes
sys.argv A list of command-line arguments passed to the Python script, the first element being the path of the program itself
Sys.executable Returns the absolute path of the Python interpreter in the current system
Sys.exit ([ARG]) Program Intermediate exit, arg=0 for normal exit
Sys.path Returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing
sys.platform return operating system platform name, Linux is linux2 , Windows is win32
Sys.stdout.write (str) Remove newline characters when exporting \n
val = Sys.stdin.readline () [:-1] Get the value minus \n line break
Sys.version Get version information for Python interpreter
    • Position parameters

[[email protected] ~]# cat scripts.py #!/usr/bin/env pythonimport sysprint (sys.argv[0]) print (sys.argv[1]) print ( SYS.ARGV[2]) [[email protected] ~]# python scripts.py canshu1 canshu2 Scripts.pycanshu1canshu

Sys.argv[0] represents the script itself, if executed with a relative path, the name of the script will be displayed, and if it is an absolute path, the script name will be displayed;

    • Program exits in the middle

Python executes the script by default at the end and then exits automatically, but if you need to exit the program halfway, you can call the sys.exit function, which is returned with an optional integer argument to the program that called it. This means that you can capture the call to the main program. sys.exit (Note: 0 is normal exit, the other is not normal, can throw abnormal events for capture!)

The result of the original script and output:

[email protected] sys]# cat sys-03.py #!/usr/bin/python# _*_ coding:utf-8 _*_import sysprint "Hello word!" Print "Your is Pythoner" [[E-mail protected] sys]# python sys-03.py Hello Word!your is Pythoner

After the script is executed, the following two pieces of content are output:

hello word!your is pythoner 

and then we're in print "Hello word! " let the program exit without executing print "Your is Pythoner"

[[email protected] sys]# cat sys-03.py #!/usr/bin/python# _*_ coding:utf-8  _*_import sysprint  "hello word!" Sys.exit () print  "Your is pythoner" [[email protected] sys]# python sys-03.py  hello word! 

PS:sys.exit out of the Python program will produce a Systemexit exception that can be done to remove the removal of the work. The default normal exit status for this optional parameter is 0, and the value for the parameter range is: 0-127. Other values are non-normal exits, and another type, shown here is the strings object type.

    • Get module path

import , _import_ When the module is imported, how does python determine if there is no such module? The
is actually based on sys.path the path to the to search for the name of the module you imported.

>>> for I in Sys.path: ... print (i) ... C:\python35\lib\site-packages\pip-8.1.1-py3.5.eggc:\python35\python35.zipc:\python35\dllsc:\python35\libc:\ Python35c:\python35\lib\site-packages
    • Get current system Platform

Linux

>>> Import sys >>> sys.platform ' linux2 '

Windows

>>> Import sys >>> sys.platform ' Win32 '

#Python标准库 #Sys


4Python standard Library Series SYS module

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.