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

查看:20
本文介绍了在 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)

这里的输入不是由 shell 读取的,而是由 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天全站免登陆