编程语言
首页 > 编程语言> > python – 从sys.argv中删除已解析的选项及其值

python – 从sys.argv中删除已解析的选项及其值

作者:互联网

我试图使用optparse(解析我的脚本的命令行选项)和fileinput(以便通过管道或文件提供数据输入的灵活性).

import optparse, fileinput

parser = OptionParser()    
parser.add_option("-v", action="store_true", dest="verbose")
(options, args) = parser.parse_args()

for line in fileinput.input:
     process(line)

但是fileinput尝试使用’-v’选项以及文件名导致’没有这样的文件或目录错误’.所以要么我需要创建fileinput args,要么从sys.argv中删除已解析的选项,但是我不知道这样做的优雅方式.有什么指针吗?

解决方法:

documentation

To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed.

所以你可以传递你从optparse获得的剩余args.

标签:python,file-io,optparse,python-2-5
来源: https://codeday.me/bug/20190530/1184038.html