在UNIX环境中运行时,防止在未引号的python脚本参数中扩展通配符 [英] Prevent expansion of wildcards in non-quoted python script argument when running in UNIX environment

查看:42
本文介绍了在UNIX环境中运行时,防止在未引号的python脚本参数中扩展通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python脚本,我想提供一个包含通配符的参数(通常),指的是我想用来处理的一系列文件.此处的示例:

I have a python script that I'd like to supply with an argument (usually) containing wildcards, referring to a series of files that I'd like to do stuff with. Example here:

#!/usr/bin/env python

import argparse
import glob 

parser = argparse.ArgumentParser()
parser.add_argument('-i', action="store", dest="i")
results = parser.parse_args()
print 'argument i is: ', results.i
list_of_matched_files = glob.glob(results.i)

在这种情况下,如果用户将引号添加到传递的参数中,则一切工作都很好,

In this case, everything works great if the user adds quotes to the passed argument like so:

./test_script.py -i "foo*.txt"

...但是,通常情况下,用户会忘记在参数中添加引号,并且当列表仅包含第一个匹配项时,用户会感到困惑,因为UNIX已经扩展了列表,而argparse仅获得了第一个列表元素.

...but often times the users forget to add quotes to the argument and are stumped when the list only contains the first match because UNIX already expanded the list and argparse only then gets the first list element.

是否有一种方法(在脚本内)可以防止UNIX在将列表传递给python之前扩展该列表?或者甚至只是为了测试参数是否不包含引号,然后警告用户?

Is there a way (within the script) to prevent UNIX from expanding the list before passing it to python? Or maybe even just to test if the argument doesn't contain quotes and then warn the user?

推荐答案

不是UNIX在进行扩展,而是shell.

Its not UNIX that is doing the expansion, it is the shell.

Bash具有选项set -o noglob(或-f),该选项可以关闭globbing(文件名扩展),但这是非标准的.

Bash has an option set -o noglob (or -f) which turns off globbing (filename expansion), but that is non-standard.

如果您授予最终用户访问命令行的权限,那么他们真的应该知道引用.例如,常用的find命令具有一个-name参数,该参数可以采用glob构造,但是必须以类似的方式对其进行引用.您的程序与其他程序没有什么不同.

If you give an end-user access to the command-line then they really should know about quoting. For example, the commonly used find command has a -name parameter which can take glob constructs but they have to be quoted in a similar manner. Your program is no different to any other.

如果用户无法处理该问题,那么也许您应该给他们一个不同的界面.您可能会写一个GUI或一个Web/HTML前端,但是这可能是最重要的.

If users can't handle that then maybe you should give them a different interface. You could go to the extreme of writing a GUI or a web/HTML front-end, but that's probably over the top.

还是为什么不提示输入文件名模式?例如,您可以使用-p选项来指示提示,例如:

Or why not prompt for the filename pattern? You could, for example, use a -p option to indicate prompting, e.g:

import argparse
import glob

parser = argparse.ArgumentParser()
parser.add_argument('-i', action="store", dest="i")
parser.add_argument('-p', action="store_true", default=False)

results = parser.parse_args()

if results.p:
    pattern = raw_input("Enter filename pattern: ")
else:
    pattern = results.i

list_of_matched_files = glob.glob(pattern)
print list_of_matched_files

(由于您的print语句,我假设使用Python 2)

(I have assumed Python 2 because of your print statement)

这里输入不是由外壳读取,而是由python读取,除非您要求,否则它将不会扩展glob构造.

Here the input is not read by the shell but by python, which will not expand glob constructs unless you ask it to.

这篇关于在UNIX环境中运行时,防止在未引号的python脚本参数中扩展通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆