python module sys module

Posted by bradles on Thu, 02 Apr 2020 15:57:44 +0200

sys.argv

Command line parameter list, the first parameter is the path of the program itself

Create a script named sys.argv.py

import sys
print(sys.argv)

The script is followed by two test names test1 and test2

C:\Users\wefbn>python F:\python\test\bin\bin.py test1 test2

Result

['F:\\python\\test\\bin\\bin.py', 'test1', 'test2']

sys.path()

View environment variables

If the modification is temporary, the system environment variable needs to be changed for permanent modification

import sys
print(sys.path)
['F:\\python\\day21_import', 'F:\\python', 'C:\\Users\\wefbn\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip', 'C:\\Users\\wefbn\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 'C:\\Users\\wefbn\\AppData\\Local\\Programs\\Python\\Python36\\lib', 'C:\\Users\\wefbn\\AppData\\Local\\Programs\\Python\\Python36', 'C:\\Users\\wefbn\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages', 'C:\\Users\\wefbn\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\win32', 'C:\\Users\\wefbn\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\win32\\lib', 'C:\\Users\\wefbn\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\Pythonwin']

sys.exit()

Exit procedure

import sys
a = 0
while a <100:
    a +=1
    if a == 44:     #Suppose the number 44 is a BUG
        sys.exit("Serious program error!!!")
Serious program error!!!

sys.version

Get version information of python interpreter program

print(sys.version)
3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]

sys.platform

Get operating system platform

print(sys.platform)
win32           #Windows
linux2          #linux

sys.stdout.write("#")

Screen printing, print is the function based on this module evolution

sys.stdout.flush()

Refresh cache

The above two comprehensive examples

Progress bar simulation

import time
for i in range(100):
    sys.stdout.write("#")
    time.sleep(0.2)
    sys.stdout.flush()

Topics: Python Windows Linux