我正在尝试使用Python 3从包含文本文档的文件夹中读取内容,这是LingSpam电子邮件垃圾邮件数据集的修改版。我期望我编写的代码能够返回所有1893个文本文档的名称,但实际上它只返回了前420个文件名。我不明白为什么它没有返回全部文件名。有任何想法吗?
if not os.path.exists('train'): # download data
from urllib.request import urlretrieve
import tarfile
urlretrieve('http://cs.iit.edu/~culotta/cs429/lingspam.tgz', 'lingspam.tgz')
tar = tarfile.open('lingspam.tgz')
tar.extractall()
tar.close()
abc = []
for f in glob.glob("train/*.txt"):
print(f)
abc.append(f)
print(len(abc))
我已经尝试更改全局参数,但仍然没有成功。
编辑:显然我的代码对于其他人来说是有效的。这里是我的输出
glob.glob("train\*.txt")
吗?或者使用glob.glob(os.path.join("train", "*.txt"))
以实现跨平台。 - user707650