你一定在windows机器上。错误是由于os.listdir()引起的。os.listdir()没有得到正确的路径。

在第3行中,您正在执行os.path.join(“archive”,目录)。

您应该加入完整的路径,包括驱动器(C:或D:),如“C:/archive/foo:或在linux上

“home/root/archive/foo”On Windows, the drive letter is not reset when an absolute path

component (e.g., r'\foo') is encountered. If a component contains a

drive letter, all previous components are thrown away and the drive

letter is reset. Note that since there is a current directory for each

drive, os.path.join("c:", "foo") represents a path relative to the

current directory on drive C: (c:foo), not c:\foo.

编辑:

您正在将列表corpus_path传递到第6行中的[os.path.join][2]。这会导致错误!用items替换corpus_path。

我在我的“D:”驱动器中创建了存档文件夹。在archive文件夹下,我创建了3个文件夹foo1、foo2和foo3。每个文件夹包含1或2个文本文件。然后我在修改后测试了你的代码。代码工作正常。

代码如下:import os

startpath = "d:archive"

corpus_path = sorted([os.path.join("d:", "archive", directories) for directories in os.listdir(startpath)])

filenames = []

for items in corpus_path:

print items

path = [os.path.join(items, fn) for fn in os.listdir(items)]

print path

输出:d:archive\foo1

['d:archive\\foo1\\foo1.txt.txt', 'd:archive\\foo1\\foo11.txt']

d:archive\foo2

['d:archive\\foo2\\foo2.txt.txt']

d:archive\foo3

['d:archive\\foo3\\foo3.txt.txt']

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐