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

2024-05-08 12:31:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在文件管理方面有问题。 我必须搜索一个扩展名为.txt的文件,但是路径每天都在变化。你知道吗

我有一个包含实际路径的另一个文件,我可以将它存储在字符串中,但是当我 给搜索算法一个窗口掉一个错误消息。你知道吗

这是我的剧本:

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:…”


Tags: 文件thepath字符串路径txt消息is
2条回答

这是为了达到目的。你知道吗

from glob import glob
from os.path import join

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

试试这个

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

/而不是\

相关问题 更多 >