Python 取消由ls-R生成的文件名

Python 取消由ls-R生成的文件名,python,shell,escaping,ls,Python,Shell,Escaping,Ls,我有一个文本文件,其中包含递归目录列表的输出,通常如下所示: ./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

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

./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
转义了(不过空格和=是两个这样的字符)。我也无法访问包含这些文件的驱动器,因此很遗憾,使用更好的命令获取列表是不可能的。

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

re.sub(r'(?<!\\)\\', '', filename)
下面是一个完整的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')

当然,您最好不要处理ls-R的输出,而是让Python直接生成文件名列表。必须有一些模块来实现这一点,但我是Perl用户,而不是Python用户,所以我无法告诉您它们的名称。使用“python目录搜索”这一术语的谷歌搜索提供了许多有用的搜索起点。@Jonathan OP已经提到他无法访问驱动器,所以像
walk
这样的东西在这里没有帮助。错过了这一点……哦,好吧……生活就是地狱。
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