[Python]python Study notes (ii)--FAQ

Source: Internet
Author: User

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
    1. >>> a = {' agitation ': 3, ' spend ': 2, ' Encro ': 1}
    2. >>> for i in A.keys ():
    3. ... i
    4. ...
    5. ' Encro '
    6. ' Spend '
    7. ' Agitation '



4,python How to create directories and files recursively

[CPP]View Plaincopy
  1. def creat_file (finame):
  2. TEMPDIR=OS.GETCWD ()
  3. Basename=os.path.basename (Finame)
  4. Dirname=os.path.dirname (Finame)
  5. Os.chdir (dirname)
  6. File (basename,' W ')
  7. Os.chdir (TempDir)
  8. def creat_dir (fname):
  9. Filename=fname
  10. N=filename.find ('/')
  11. if n>0:
  12. dir= filename[0:filename.find ('/') +1]
  13. Print Dir
  14. If not os.path.exists (dir):
  15. Os.makedirs (dir,mode=0777)
  16. Os.chdir (dir)
  17. Dirleft=filename[filename.find ('/') + 1:]
  18. Creat_dir (Dirleft)
  19. Else:
  20. dir= filename[0:filename.find ('/') +1]
  21. Dirleft=filename[filename.find ('/') + 1:]
  22. Os.chdir (dir)
  23. 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

Related Article

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.