gpt4 book ai didi

python - 迭代python中的特定目录

转载 作者:行者123 更新时间:2023-12-01 05:44:46 25 4
gpt4 key购买 nike

我正在尝试读取目录中的目录以获得对特定文件的访问权限。

现在,虽然我可以进入所有子目录来获取我想要的目录(基本上所有名称为“sent”和“inbox”的目录,但我无法打开这些文件夹中的所有文件。

 for root, dirs,  files in os.walk("/home/nandita/Downloads/enron_mail_20110402"):
if len(dirs) != 0:
for dir in dirs:
if dir == "sent" or dir == "inbox":
rootd = os.path.join(root, dir)
print rootd
#for root, dirs, files in os.walk("/home/nandita/Downloads/enron_mail_20110402/"+dir):
for filename in files:
filename = os.path.join(rootd, filename)
print filename
with open(filename, 'r') as f:
head,sub,authors,recipients = [f.readline().strip() for i in range(4)]
data=f.read()

现在我可以读取所有子目录了:

> /home/nandita/Downloads/enron_mail_20110402/maildir/farmer-d/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/williams-w3/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/symes-k/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/lavorato-j/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/lavorato-j/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/arnold-j/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/arnold-j/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/lewis-a/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/lewis-a/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/neal-s/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/neal-s/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/mcconnell-m/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/mcconnell-m/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/skilling-j/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/skilling-j/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/shankman-j/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/shankman-j/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/reitmeyer-j/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/ermis-f/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/ermis-f/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/kuykendall-t/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/kuykendall-t/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/hendrickson-s/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/scott-s/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/scott-s/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/carson-m/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/carson-m/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/causholli-m/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/bass-e/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/bass-e/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/cuilla-m/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/cuilla-m/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/geaccone-t/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/shapiro-r/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/shapiro-r/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/solberg-g/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/slinger-r/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/taylor-m/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/taylor-m/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/cash-m/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/cash-m/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/ward-k/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/ward-k/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/mann-k/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/mann-k/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/giron-d/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/giron-d/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/saibi-e/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/fossum-d/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/grigsby-m/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/grigsby-m/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/davis-d/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/davis-d/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/delainey-d/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/delainey-d/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/sturm-f/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/sturm-f/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/schoolcraft-d/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/germany-c/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/germany-c/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/stclair-c/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/horton-s/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/horton-s/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/keavey-p/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/keavey-p/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/dean-c/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/dean-c/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/bailey-s/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/benson-r/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/benson-r/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/pereira-s/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/pereira-s/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/sanchez-m/inbox
/home/nandita/Downloads/enron_mail_20110402/maildir/lenhart-m/sent
/home/nandita/Downloads/enron_mail_20110402/maildir/lenhart-m/inbox

但是即使我在文件中输入文件名,我也什么也没得到。有什么指点吗?

最佳答案

仅当目录中还包含目录时才搜索文件。文件的 for 循环应与 dirs 长度测试处于同一级别

for root, dirs, files in os.walk("/home/nandita/Downloads/enron_mail_20110402"):
dir = os.path.basename(root)
if dir == "sent" or dir == "inbox":
print root
for filename in files:
filename = os.path.join(root, filename)
print filename
with open(filename, 'r') as f:
head,sub,authors,recipients = [f.readline().strip() for i in range(4)]
data=f.read()

关于python - 迭代python中的特定目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16434383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com