取消转义 ls -R 生成的文件名
我有一个文本文件,其中包含递归目录列表的输出,通常如下所示:
./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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定是否有内置的,但可以使用一个简单的正则表达式。
这将删除所有反斜杠(除了另一个反斜杠后面的反斜杠之外)。这似乎是当您尝试在终端上回显这些值时的行为(我只在 bash 中对此进行了测试)。
这是一个完整的 python 示例:
输出:
I'm not sure if there's built-in for this, but a simple regex could be used.
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).Here's a complete python example:
Output: