1,python if __name__ = = "__main__": the Role of
Determine whether the current module is directly invoked or directly executed, if the current module is called, then if __name__ = = "__main__": After the statement will not be executed, if the current pattern is executed directly, then if __name__ = = "__main__ ": The following statement will be executed to ~
2,python processing binary data using struct structure body
Python processes binary data using struct structure body
3, the values in the dictionary are unordered, as in the following example:
[CPP]View Plaincopy
- >>> a = {' agitation ': 3, ' spend ': 2, ' Encro ': 1}
- >>> for i in A.keys ():
- ... i
- ...
- ' Encro '
- ' Spend '
- ' Agitation '
4,python How to create directories and files recursively
[CPP]View Plaincopy
- def creat_file (finame):
- TEMPDIR=OS.GETCWD ()
- Basename=os.path.basename (Finame)
- Dirname=os.path.dirname (Finame)
- Os.chdir (dirname)
- File (basename,' W ')
- Os.chdir (TempDir)
- def creat_dir (fname):
- Filename=fname
- N=filename.find ('/')
- if n>0:
- dir= filename[0:filename.find ('/') +1]
- Print Dir
- If not os.path.exists (dir):
- Os.makedirs (dir,mode=0777)
- Os.chdir (dir)
- Dirleft=filename[filename.find ('/') + 1:]
- Creat_dir (Dirleft)
- Else:
- dir= filename[0:filename.find ('/') +1]
- Dirleft=filename[filename.find ('/') + 1:]
- Os.chdir (dir)
- Creat_dir (dirleft) <strong> </strong>
5,python how to invoke a shell script
Os.system (' mkdir SS ')
Os.system () The method of passing variables is as follows,
Log= "Main.log"
Os.system (' cat%s | grep nvram '%log)
6, how to call a regular expression in Python
Python's Re module
The following code means finding a string in a string that starts with uppercase or lowercase letters
Cfilename= ' ABDAA123AA '
M=re.search (' [a-z,/,a-z,_]+ ', cFileName)
Print M.group (0)
7,python How to use all variables
Define the global variables in a separate module:
#gl. py
Gl_1 = ' Hello '
gl_2 = ' World '
Use in other modules
#a. py
Import GL
def Hello_world ()
Print Gl.gl_1, gl.gl_2
For variable sharing between different files
8, cite all the files in the current directory and print
Listfile=os.listdir (OS.GETCWD ())
For line in ListFile:
Print Line
9,python use with and or ~
and and OR keywords ~
10. Parsing command-line arguments using the Getopt module
Examples are as follows:
Parameter args is generally sys.argv[1:]
Shortopts short Format (-)
On the command line, enter:
Python test.py-i 127.0.0.1-p 80 55 66
Python test.py--ip=127.0.0.1--port=80 55 66
The following code:
Try
Options,args = Getopt.getopt (sys.argv[1:], "HP:I:", ["Help", "ip=", "port="])
Except Getopt. Getopterror:
Sys.exit ()
For Name,value in Options:
If name in ("-H", "--help"):
Usage ()
If name in ("-I", "--ip"):
print ' IP is----', value
If name in ("-P", "--port")
print ' port is----', value
[Python]python Study notes (ii)--FAQ