sys:
Introduction: it mainly includes functions related to the interaction between python compiler and system.
Common functions:
import sys print(sys.argv)#Name of this file,Parameters when the program is already running #[As in the command window python3 mysys.py Parameter 1 Parameter 2] #Then parameter 1 is sys.argv[1],And so on print(sys.version)#python Version number print(sys.path)#Returns the search path of the module, which is used during initialization PYTHONPATH Value of environment variable # sys.exit(0)#Quit the program midway. When the parameter is not 0, a SystemExit exception will be thrown
sys.stdout.write()#Print on screen sys.stdout.flush()#Refresh standard buffer
os:
Introduction: This module provides a convenient way to use operating system functions.
Common functions:
import os print("-------getcwd()Get current directory-------") print(os.getcwd()) print("-------chdir()Change the current directory-------") # print(os.chdir("c://users"))#c:\\users r'c:\users' # print(os.getcwd()) print("------ . .. --------") print(os.curdir) #Print out . print(os.pardir) #Print out .. print("-------makedirs Create directory recursively-------") #os.makedirs(r"c:\a\b\c") #To create c, create a if a does not exist, and b if b does not exist print("-----remodir Delete directory recursively---------") #os.removedirs(r"c:\a\b\c") #Clear the empty folder from c to A. if a and B are also empty, they will also be deleted. print("------mkdir Create directory--------") # os.mkdir('c://a') print("--------listdir Lists all files and subdirectories in the specified directory------") print(os.listdir()) print("--------remove Delete files------") # print(os.remove('c://newfile')) print("-------rename File rename-------") # os.rename('oldname','newname') print("-------stat Get file or directory information-------") print(os.stat('.')) print("------sep Path dividers for output operating system characteristics--------") print(os.sep) print("-----linesep Output line terminator of current platform---------") list1=[] list1.append(os.linesep) print(list1) print("------pathsep Output the string used to split the file--------") print(os.pathsep) print("----------name Output operation platform----------") # print(os.name)#nt print("-------system implement shell command-------------") print(os.system("dir")) print("----------path About file and directory operations----------") # print(os.path.abspath(__file__))###Return to absolute path print(os.path.split(os.path.abspath(__file__)))##Cut paths into directory and file names print(os.path.dirname(os.path.abspath(__file__)))#Take pathname only print(os.path.dirname(__file__))###__file? Includes the full path name and also the absolute path print(os.path.basename(__file__))#File name only print(os.path.exists("c://a"))#Determine whether the path exists, regardless of directory or file print(os.path.isabs(__file__))#Determine whether it is an absolute path print(os.path.isfile("c://amd"))#Determine whether it is a document print(os.path.join(r'c:',r'\a.txt'))#Combined absolute path print("----------environ Get all environment variables of the current system----------") print(os.environ) print("---------popen() Method to open a pipe from a command-----------") print(os.popen('dir').read())##Mainly used to process the return results of executing commands print("Get process number".center(50,'-')) print(os.getpid())#Get current process number print(os.getppid())#Get parent process number
Be careful:
In some systems, the main difference between os.system and os.popen is that the return value of the former is the exit status code of the script, and the return value of the latter is a file descriptor storing the output content during the execution of the script.
time:
Introduction: contains functions about time
Common functions:
import time print("--------time stamp-------------") print("time stamp time:",time.time())#time stamp time: 1516435471.756463 print("----------Structured time(tm_year=2018, tm_mon=1.....-----------") print("struct_time:",time.gmtime(time.time()))#tm_year=2018, tm_mon=1......... print("timestamp->struct_time:",time.gmtime())#UTC time print("local_time:",time.localtime())#Local time zone time print("struct_time->timstamp:",time.mktime(time.gmtime()))#Structured time-->time stamp print("----------ctime,asctime--------") print("string_time:",time.ctime())###String time mon Feb 5 01:02:06 2018 print("asctime:",time.asctime())###String time mon Feb 5 01:02:06 2018 print("----------format_time Format time struct_time-----------") #Structured time to format time:%Y representative year,%m representative month,%d representative day, %H representative hour,%M representative minute,%S representative second #Will only replace%Y Equal characters do not replace characters without corresponding meaning print("struct_time -> format_time:\n", time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())) y=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) #Format time to structure time print("format_time -> struct_time:\n",time.strptime(y,"%Y-%m-%d %H:%M:%S")) print("------------year--------------") print("year:",time.localtime().tm_year)
random:
Introduction: store functions about "random"
Common functions:
import random print("---------0 To 1,Random floating point value-----------") print(random.random()) print("------------Random value from range,1<=x<=2--------") print(random.randint(1,2)) print("------------Random value from the specified range--------") print(random.randrange(1,3)) print("------------From a sequence, random values--------") print(random.choice("hello"))#Random value from sequence print(random.choice([0,11,3,99])) print("------------From the sequence, randomly take the specified value--------") print(random.sample('heigo',2))# print("------------Random floating point value, start,end--------") print(random.uniform(1,2))#start,end print("-------Shuffle the cards,Disorder sorting-----") l=[0,3,4,5,67,9] random.shuffle(l) print(l)