User Tools

Site Tools


python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python [2012/07/30 02:44] – created memeruizpython [2021/02/01 05:55] (current) – external edit 127.0.0.1
Line 4: Line 4:
  
 ===== How to read command line arguments ===== ===== How to read command line arguments =====
 +
 +==== Simple approach ====
 +
  
   import sys   import sys
   print sys.argv # this is an array containing all command line arguments inclusive the program name   print sys.argv # this is an array containing all command line arguments inclusive the program name
  
 +
 +==== OptParse ====
 +
 +  from optparse import OptionParser
 +  parser = OptionParser()
 +  parser.add_option("-f", "--file", dest="filename",
 +                    help="write report to FILE", metavar="FILE")
 +  parser.add_option("-q", "--quiet",
 +                    action="store_false", dest="verbose", default=True,
 +                    help="don't print status messages to stdout")
 +  (options, args) = parser.parse_args()
 +
 +===== How to make a python source code file executable =====
 +
 +Code example:
 +
 +  #!/usr/bin/env python # this finds the default python executable defined by the current environment (PATH).
 +  print "test"
 +
 +In the console don't forget to give executable permissions:
 +
 +  chmod +x example.py
 +
 +===== Important functions =====
 +
 +  os.walk("dir")
 +  os.*
 +  sys.argv
 +
 +===== Important modules =====
 +
 +  optparse
 +  multiprocessing
 +  numpy
  
python.1343616262.txt.gz · Last modified: 2021/02/01 05:55 (external edit)