我正在尝试读取目录中的目录以获取对特定文件的访问权限。
现在,虽然我能够进入所有子目录以获取我想要的目录(基本上所有名称为“已发送”和“收件箱”的目录,但我无法再打开这些文件夹中的所有文件。
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
但即使我在文件中做文件名,我什么也得不到。有什么指针吗?
答案 0 :(得分:2)
仅当目录还包含目录时才搜索文件。您的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()