Python 为什么我的glob.glob循环没有遍历文件夹中的所有文本文件?
我正在尝试使用Python3从包含文本文档的文件夹中读取。具体来说,这是对LingSpam电子邮件数据集的修改。我希望我编写的代码能够返回所有1893个文本文档名,但是,代码会返回前420个文件名。我不明白为什么它的文件名总数不足。有什么想法吗Python 为什么我的glob.glob循环没有遍历文件夹中的所有文本文件?,python,python-3.x,text,glob,Python,Python 3.x,Text,Glob,我正在尝试使用Python3从包含文本文档的文件夹中读取。具体来说,这是对LingSpam电子邮件数据集的修改。我希望我编写的代码能够返回所有1893个文本文档名,但是,代码会返回前420个文件名。我不明白为什么它的文件名总数不足。有什么想法吗 if not os.path.exists('train'): # download data from urllib.request import urlretrieve import tarfile urlretrieve('http:
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))
我尝试过更改全局参数,但仍然没有成功
编辑:显然,我的代码适用于除我以外的所有人。这是我的成功!问题是
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语句,也会得到同样的结果。您的代码对我来说很好:您尝试过使用绝对路径吗?我也确认了。这对我有用。这很奇怪。为什么它对你们的作用和对我的不同呢?读标题为“glob.glob gloop”。为娱乐性标题投票