简体   繁体   English

如何在python3中获得glob表达式的反斜杠转义版本

[英]how do I get backslashed escaped version of a glob expression in python3

I am building commands in Python, including a glob expression that also contain whitespace characters.我正在 Python 中构建命令,包括一个也包含空白字符的 glob 表达式。

I read that the suggested way is using shlex.quote , but it only works for actual resoved pathnames, because enclosing the expression within ' or " also blocks the shell glob expansion .我读到建议的方法是使用shlex.quote ,但它仅适用于实际解析的路径名,因为将表达式包含在'"中也会阻止 shell glob 扩展

Simplest possible example:最简单的例子:

touch 'a file.txt'
ls 'a file.*' # this gives an error because the glob is not resolved
# shlex.quote achieves the same result
python3 -c "import os; from shlex import quote; os.system('ls '+quote('a file.*'))"    
ls a\ file.* # this is what I need

I know that I can use any replacement technique to replace spaces ' ' with '\ ' in that string (eg regexps to name one), but I thought that should there already exist a suitable function, I should first look for it rather than writing my own code...我知道我可以使用任何替换技术在该字符串中用'\ '替换空格' ' (例如,命名一个的正则表达式),但我认为如果已经存在合适的 function,我应该首先寻找它而不是写我自己的代码...

The real case真实案例

The commands I am composing, should affect multiple files whose pathname is like (think of it separated by underscores '_' ):我正在编写的命令应该影响路径名类似的多个文件(认为它由下划线'_'分隔):

dir/WrongID_Another ID With Spaces_other-stuff-with-hyphens.bmp

I want to obtain commands like:我想获得如下命令:

rename "s/^WrongID_/RightId_/" dir/WrongID_Another\ ID\ With\ Spaces_*.*

... where dir , WrongID , Another ID With Spaces , RightID , are read from a table ...其中dirWrongIDAnother ID With SpacesRightID ,是从表中读取的

Many such commands are written to a single file, a script which I will then feed to bash.许多这样的命令被写入一个文件,一个脚本,然后我将把它提供给 bash。

Are you perhaps looking for您是否正在寻找

while read -p dir wrong rest right; do
    rename "s/^${wrong}_/${right}_/" "$dir/${wrong}_${rest}_"*.*
done <<\____
    one    few     other   many
    two    theyre  stuff   there
    three  grammer correct grammar
____

The braces are necessary to disambiguate ${wrong}_ (the variable wrong followed by an underscore) from $wrong_ (the variable wrong_ with an underscore).大括号对于区分${wrong}_ (变量wrong后跟下划线)和$wrong_ (变量wrong_带下划线)是必要的。

I don't think Python is necessary or useful here, though as you have found, shlex.quote() does provide the functionality you are asking for.我不认为 Python 在这里是必要的或有用的,尽管正如您所发现的, shlex.quote()确实提供了您所要求的功能。 If you want to drive this from Python, it's much better to avoid the shell entirely.如果您想从 Python 驱动它,最好完全避免 shell。

import subprocess
import glob

for dir, wrong, rest, right in (
        ('one',   'few',     'other',   'many'),
        ('two',   'theyre',  'stuff',   'there'),
        ('three', 'grammer', 'correct', 'grammar')):
    subprocess.run(['rename', 's/^{0}_/{1}_/'.format(wrong, right)] +
        glob.glob('{0}/{1}_{2}_*.*'.format(dir, wrong, rest))])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM