Python 文件路径中的转义空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18136706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:58:39  来源:igfitidea点击:

Escape space in filepath

pythonregex

提问by user2666063

I'm trying to write a python tool that will read a logfile and process it

我正在尝试编写一个 python 工具来读取日志文件并处理它

One thing it should do is use the paths listed in the logfile (it's a logfile for a backup tool)

它应该做的一件事是使用日志文件中列出的路径(它是备份工具的日志文件)

/Volumes/Live_Jobs/Live_Jobs/*SCANS\ and\ LE\ Docs/_LE_PROOFS_DOCS/JEM_lj/JEM/0002_OXO_CorkScrew/3\ Delivery/GG_Double\ Lever\ Waiters\ Corkscrew_072613_Mike_RETOUCHED/gg_3110200_2_V3_Final.tif

Unfortunately the paths that I'm provided with aren't appropriately escaped and I've had trouble properly escaping in python. Perhaps python isn't the best tool for this, but I like it's flexibility - it will allow me to extend whatever I write

不幸的是,我提供的路径没有被适当地转义,我在 python 中正确转义时遇到了问题。也许python不是最好的工具,但我喜欢它的灵活性——它可以让我扩展我写的任何东西

Using the regex escape function escapes too many characters, pipes.quote method doesn't escape the spaces, and if I use a regex to replace ' ' with '\ ' I end up getting

使用正则表达式转义函数转义太多字符,pipes.quote 方法不会转义空格,如果我使用正则表达式将 ' ' 替换为 '\ ' 我最终得到

/Volumes/Live_Jobs/Live_Jobs/*SCANS\ and\ LE\ Docs/_LE_PROOFS_DOCS/JEM_lj/JEM/0002_OXO_CorkScrew/3\ Delivery/GG_Double\ Lever\ Waiters\ Corkscrew_072613_Mike_RETOUCHED/gg_3110200_2_V3_Final.tif

which are double escaped and wont pass to python functions like os.path.getsize().

这是双重转义,不会传递给像os.path.getsize().

What am I doing wrong??

我究竟做错了什么??

采纳答案by abarnert

If you're reading paths out of a file, and passing them to functions like os.path.getsize, you don't need to escape them. For example:

如果您从文件中读取路径,并将它们传递给像 的函数os.path.getsize,则不需要对它们进行转义。例如:

>>> with open('name with spaces', 'w') as f:
...     f.write('abc\n')
>>> os.path.getsize('name with spaces')
4

In fact, there are only a handful of functions in Python that need spaces escaped, either because they're passing a string to the shell (like os.system) or because they're trying to do shell-like parsing on your behalf (like subprocess.foowith an arg string instead of an arg list).

实际上,Python 中只有少数函数需要转义空格,要么是因为它们将字符串传递给 shell(例如os.system),要么是因为它们试图代表您进行类似 shell 的解析(例如subprocess.foo使用arg 字符串而不是 arg 列表)。



So, let's say logfile.txtlooks like this:

所以,让我们说logfile.txt看起来像这样:

/Volumes/My Drive/My Scans/Batch 1/foo bar.tif
/Volumes/My Drive/My Scans/Batch 1/spam eggs.tif
/Volumes/My Drive/My Scans/Batch 2/another long name.tif

… then something like this will work fine:

......然后这样的事情会正常工作:

with open('logfile.txt') as logf:
    for line in logf:
        with open(line.rstrip()) as f:
            do_something_with_tiff_file(f)

Noticing those *characters in your example, if these are glob patterns, that's fine too:

注意*您的示例中的这些字符,如果这些是 glob 模式,那也很好:

with open('logfile.txt') as logf:
    for line in logf:
        for path in glob.glob(line.rstrip()):
            with open(path) as f:
                do_something_with_tiff_file(f)


If your problem is the exact opposite of what you described, and the file is full of strings that areescaped, and you want to unescape them, decode('string_escape')will undo Python-style escaping, and there are different functions to undo different kinds of escaping, but without knowing what kind of escaping you want to undo it's hard to say which function you want…

如果您的问题与您所描述的完全相反,并且文件中充满了转义的字符串,而您想对它们进行转义,decode('string_escape')则将撤消 Python 风格的转义,并且有不同的函数可以撤消不同类型的转义,但是在不知道您想要撤消哪种转义的情况下,很难说您想要哪种功能......

回答by Aakash Anuj

Try this:

尝试这个:

  myfile = open(r'c:\tmp\junkpythonfile','w')

The 'r' stands for a raw string.

'r' 代表原始字符串。

You could also use \ like

你也可以使用\像

myfile = open('c:\tmp\junkpythonfile','w')