在optparse中,我使用了兩個標記,並且我想獲得標記後的下一個值。我的要求是我可以給任何選項,甚至兩個選項,所以我使用這些選項像標誌。我正在使用parser.parse_args()來獲取值,但問題是我得到的輸出包括一些括號。從optparse python獲取最後一個參數
#!/usr/bin/python
import optparse
import sys
import os.path
parser = optparse.OptionParser()
parser.add_option('-p', action='store_true', default=False, dest='DEST1', help="Some info")
parser.add_option('-k', action='store_true', default=False, dest='DEST2', help="Some info")
options, infile = parser.parse_args()
print infile
O /文件的P是
/myfile.py -p abc1
['abc1']
我只需要文件名作爲ABC1
你使用的是什麼版本的Python?我問,因爲'argparse'更好,但需要Python 2.7或更高版本。 – 2014-10-20 05:05:56
我正在使用Python 2.6.6,如果它能工作,我甚至可以使用argparse。 – 2014-10-20 05:07:28