取消转义 ls -R 生成的文件名

发布于 2024-12-23 09:42:33 字数 536 浏览 3 评论 0原文

我有一个文本文件,其中包含递归目录列表的输出,通常如下所示:

./subfolder/something with spaces:
something\ with\ spaces.txt*
something\ with\ spaces.dat*

./subfolder/yet another thing:
yet\ another\ thing.txt*
yet\ another\ thing.dat*

我需要获取每个 .txt 文件的完整路径列表:

./subfolder/something with spaces/something with spaces.txt
./subfolder/yet another thing/yet another thing.txt

我几乎已经找到了解决方案,但最好的解决方案是什么在Python中取消转义文件名?我不知道 ls -R 转义了哪些字符(不过空格和 = 是两个这样的字符)。我也无权访问包含这些文件的驱动器,因此不幸的是,使用更好的命令来获取列表是不可能的。

I have a text file containing the output of a recursive directory listing that generally looks like this:

./subfolder/something with spaces:
something\ with\ spaces.txt*
something\ with\ spaces.dat*

./subfolder/yet another thing:
yet\ another\ thing.txt*
yet\ another\ thing.dat*

I need to get a list of the full paths to each .txt file:

./subfolder/something with spaces/something with spaces.txt
./subfolder/yet another thing/yet another thing.txt

I've almost got a solution for this, but what's the best solution for unescaping the filenames in Python? I don't know exactly what characters ls -R escaped (space and = are two such characters, though). I don't have access to the drive containing these files, either, so using a better command to obtain the list is out of the question, unfortunately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归属感 2024-12-30 09:42:33

我不确定是否有内置的,但可以使用一个简单的正则表达式。

re.sub(r'(?<!\\)\\', '', filename)

这将删除所有反斜杠(除了另一个反斜杠后面的反斜杠之外)。这似乎是当您尝试在终端上回显这些值时的行为(我只在 bash 中对此进行了测试)。

bash-3.2$ echo foo\\bar
foo\bar
bash-3.2$ echo foo\ bar
foo bar
bash-3.2$ echo foo\=bar
foo=bar

这是一个完整的 python 示例:

import re

def unescape(filename):
    return re.sub(r'(?<!\\)\\', '', filename)

print unescape(r'foo\ bar')
print unescape(r'foo\=bar')
print unescape(r'foo\\bar')

输出:

foo bar
foo=bar
foo\bar

I'm not sure if there's built-in for this, but a simple regex could be used.

re.sub(r'(?<!\\)\\', '', filename)

This would remove all backslashes (except for those following another backslash). This seems to be the behavior when you try and echo these values on the terminal (I've only tested this in bash).

bash-3.2$ echo foo\\bar
foo\bar
bash-3.2$ echo foo\ bar
foo bar
bash-3.2$ echo foo\=bar
foo=bar

Here's a complete python example:

import re

def unescape(filename):
    return re.sub(r'(?<!\\)\\', '', filename)

print unescape(r'foo\ bar')
print unescape(r'foo\=bar')
print unescape(r'foo\\bar')

Output:

foo bar
foo=bar
foo\bar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文