[英]Why is my glob.glob loop not iterating through all text files in folder?
我试图从包含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参数但仍然没有成功。
编辑:显然我的代码适用于除我之外的所有人。 这是我的输出
成功! 问题是
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()
没有if语句就会有相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.