L If __name__ = = ' __main__ ': # when running as a script
n __name__ detection is only ture when the file is running , is false when it isloaded , and is determined when it isrun as a top-level script ture
N run: Command line, click the file icon, use IDLE GUI
N Each Python module has a built-in __name__ variable that, when it is run as a program and not imported as a library,will be set to the __main__ string by Python.
L command-line arguments
N Example
u Import sys
u Print sys.argv
u If you enter python test.py 1 2 3
u Output [' test.py ', ' 1 ', ' 2 ', ' 3 ']
l sys module
N Example
u Import sys
u print dir (SYS)// display sys contents
u Print len (dir (SYS))// display number of bars
N most of the system interfaces in Python are concentrated in these two modules
u sys
u os
u Other Modules
l glob for file name extension
l socket for network connection and interprocess communication IPC
l Threading, _thread, queue for running and synchronizing concurrent threads
l time, Timeit to get the details about the system's timing
N View Documents
u Import sys
u print (sys.__doc__)
N Sys.path
u a list of directory name strings, each of which represents a running the true search path for the Python interpreter
u when the interpreter starts, the list is based on the PYTHONPATH settings for initialization
N sys.modules
u sys.modules is a dictionary in which each module that your Python session or process imports has a name:module entry
N Other Tools
u sys.argv is displayed as a command-line argument for a list consisting of strings
u sys.stdin sys.stdout sys.stderr Standard Flow
u sys.exit Force exit by invoking
l os module
N Compared to sys is the larger one
N used in operational practice as a portable interface for computer system calls, scripts written with OS and Os.path can often be run on other platforms without modification
N Common Module Tools
u Shell variable os.environ
u Run the program Os.system, Os.popen, OS.EXECV, OS,SPAWNV
u derivation process os.fork, Os.pipe, Os.waitpid, Os,kill
u file descriptor Os.open, Os.read, Os.write
u file processing os.remove, Os.rename, Os.mkfifo, Os.mkdir, Os.rmdir
u Management Tools OS.GETCWD, Os.chdir, Os.chmod, Os.getpid, Os.listdir
u Porting Tools
u Path Name Tool
u Example:
l Import os
l os.getpid ()
L Output 3784
L also open a cmd,#tasklist, find python.exe discovery process pid is 3784
u Example:
l Import os
l os.getcwd ()
L output the current working directory
u Example:
l Import os
l os.system (' dir ')
L Output dir output under normal conditions
u Example:
l Import os
l test = os.popen (' type helloshell.py '). Read ()
L Put the output in test after executing the Shell command
L String Method Base Operation
N Example
u mystr = ' Heheyouhehe '
u Print mystr.find (' you ')// search for specified string
u print mystr.replace (' hehe ', ' haha ')// string substitution
u print ' hehe ' in mystr// Determine if there is a specified string
u Output:
L 4
L Hahayouhaha
L True
N Example
u mystr = ' \ t Ni \ n '
u mystr.strip ()// crop blank separator
u mystr.rstrip ()// crop right blank separator only
u mystr.upper ()// capitalize, but note that mystr itself does not change
u mystr.lower ()// variable lowercase
u mystr.isalpha ()
u mystr.isdigit ()
N Example
u mystr = ' Abcxxdefxxghi '
u delim = ' 88 '
u Print delim.join (mystr.split (' xx '))
u Output Abc88def88ghi
u Note the use of the split method and the Join method
L Basic file Operations
n file = open (' E:\\spam.txt ', ' W ')
N file.write ((' Spam ' * 5) + ' \ n ')
n file.close ()
n file2 = open (' E:\\spam.txt ')
n Text = File.read ()
N Print text
Python system programming (self-Learning Python series notes-3) (not regularly updated)