python:pythonbasicexamples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
python:pythonbasicexamples [2020/02/26 15:57] – [Read and Update File] admin | python:pythonbasicexamples [2020/02/26 16:08] – [Python Command Line Arguments] admin | ||
---|---|---|---|
Line 218: | Line 218: | ||
with open(filename, | with open(filename, | ||
f.write(content) | f.write(content) | ||
+ | </ | ||
+ | ===== Python Command Line Arguments ===== | ||
+ | refer: https:// | ||
+ | |||
+ | Consider we want to pass two file names through command line and we also want to give an option to check the usage of the script. Usage of the script is as follows − | ||
+ | <code bash> | ||
+ | usage: test.py -i < | ||
+ | </ | ||
+ | Here is the following script to test.py − | ||
+ | <code python> | ||
+ | # | ||
+ | |||
+ | import sys, getopt | ||
+ | |||
+ | def main(argv): | ||
+ | | ||
+ | | ||
+ | try: | ||
+ | opts, args = getopt.getopt(argv," | ||
+ | | ||
+ | print ' | ||
+ | sys.exit(2) | ||
+ | for opt, arg in opts: | ||
+ | if opt == ' | ||
+ | print ' | ||
+ | | ||
+ | elif opt in (" | ||
+ | | ||
+ | elif opt in (" | ||
+ | | ||
+ | print 'Input file is "', | ||
+ | print ' | ||
+ | |||
+ | if __name__ == " | ||
+ | | ||
+ | </ | ||
+ | Now, run above script as follows − | ||
+ | * Option1:< | ||
+ | $ test.py -h | ||
+ | </ | ||
+ | test.py -i < | ||
+ | </ | ||
+ | * Option2:< | ||
+ | $ test.py -i inputfile | ||
+ | </ | ||
+ | Input file is " inputfile | ||
+ | Output file is " | ||
</ | </ |
python/pythonbasicexamples.txt · Last modified: 2022/10/29 16:15 by 127.0.0.1