我必须搜索扩展名为.txt的文件,但路径每天都在变化

时间:2012-10-03 17:40:57

标签: python

我的文件管理有问题。 我必须搜索扩展名为.txt的文件,但路径每天都在变化。

我有另一个包含实际路径的文件,我可以将它存储在一个字符串中,但是当我 给搜索算法windows下一条错误信息。

这是我的剧本:

path= 'c:\..... this is the path what I get back from an another script'

os.chdir(path)
for files in os.listdir("."):``
    if files.endswith(".txt"):
        print files

错误消息:WindowsError:[错误3]系统找不到指定的路径:'c:....'

2 个答案:

答案 0 :(得分:0)

试试这个

path= 'c:/..... this is the path what I get back from an another script'

os.chdir(path)
for files in os.listdir("."):``
    if files.endswith(".txt"):
        print files

/代替\

答案 1 :(得分:0)

这可以解决问题。

from glob import glob
from os.path import join

path = 'c:\\test'
print glob(join(path, '*.txt'))