英文:
Python won't overwrite files / directory if already exists
问题
我正在使用Python来从Gmail下载图像附件,使用以下脚本:
#!/usr/bin/env python3
# 读取邮件并将附件存储在attachments目录中
import email
import getpass, imaplib
import os
import sys
import time
detach_dir = '.'
if 'attachments' not in os.listdir(detach_dir):
os.mkdir('/var/www/html/attachments')
userName = '输入你的Gmail帐户名'
passwd = '输入你的密码'
try:
imapSession = imaplib.IMAP4_SSL('imap.gmail.com', 993)
typ, accountDetails = imapSession.login(userName, passwd)
if typ != 'OK':
print('无法登录!')
raise Exception
imapSession.select('Inbox')
typ, data = imapSession.search(None, 'SUBJECT', '"DASHBOARD"')
if typ != 'OK':
print('搜索收件箱时出错。')
raise Exception
# 遍历所有邮件
for msgId in data[0].split():
typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
if typ != 'OK':
print('获取邮件时出错。')
raise Exception
emailBody = messageParts[0][1]
mail = email.message_from_bytes(emailBody)
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join(detach_dir, '/var/www/html/attachments', fileName)
if not os.path.isfile(filePath):
print(fileName)
print(filePath)
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
# 移动邮件到已处理部分开始
imapSession.select(mailbox='inbox', readonly=False)
resp, items = imapSession.search(None, 'All')
email_ids = items[0].split()
for email in email_ids:
latest_email_id = email
# 移动到已处理
result = imapSession.store(latest_email_id, '+X-GM-LABELS', 'Processed')
if result[0] == 'OK':
# 从收件箱删除
imapSession.store(latest_email_id, '+FLAGS', '\\Deleted')
# 移动邮件到已处理结束
imapSession.close()
imapSession.logout()
except:
print('未下载任何附件')
我遇到了以下错误:
Traceback (most recent call last):
File "/home/jenns/get_images_from_emails.py", line 12, in <module>
os.mkdir('/var/www/html/attachments')
FileExistsError: [Errno 17] File exists: '/var/www/html/attachments'
我注意到python3不在#!/usr/bin/env python3
中,已将其更改为#!/usr/bin python3
,但结果并没有改变。
我知道Gmail方面正常工作,因为附件正在移动到"Processed"文件夹,并且邮件已从我的收件箱中删除。
我还使用了sudo chmod 775 '/var/www/html/attachments'
,但问题未得到解决。为什么文件未能使用get_images_from_emails.py
脚本下载?
英文:
I'm using Python to download image attachments from gmail using the following script:
#!/usr/bin/env python3
# read emails and detach attachment in attachments directory
import email
import getpass, imaplib
import os
import sys
import time
detach_dir = '.'
if 'attachments' not in os.listdir(detach_dir):
os.mkdir('/var/www/html/attachments')
userName = 'enter-your-gmail-account-name-here'
passwd = 'enter-your-password-here'
try:
imapSession = imaplib.IMAP4_SSL('imap.gmail.com',993)
typ, accountDetails = imapSession.login(userName, passwd)
if typ != 'OK':
print ('Not able to sign in!')
raise Exception
imapSession.select('Inbox')
typ, data = imapSession.search(None, 'SUBJECT', '"DASHBOARD"')
if typ != 'OK':
print ('Error searching Inbox.')
raise Exception
# Iterating over all emails
for msgId in data[0].split():
typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
if typ != 'OK':
print ('Error fetching mail.')
raise Exception
emailBody = messageParts[0][1]
mail = email.message_from_bytes(emailBody)
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join(detach_dir, '/var/www/html/attachments', fileName)
if not os.path.isfile(filePath) :
print (fileName)
print (filePath)
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
#MOVING EMAILS TO PROCESSED PART BEGINS HERE
imapSession.select(mailbox='inbox', readonly=False)
resp, items = imapSession.search(None, 'All')
email_ids = items[0].split()
for email in email_ids:
latest_email_id = email
#move to processed
result = imapSession.store(latest_email_id, '+X-GM-LABELS', 'Processed')
if result[0] == 'OK':
#delete from inbox
imapSession.store(latest_email_id, '+FLAGS', '\\Deleted')
#END OF MOVING EMAILS TO PROCESSED
imapSession.close()
imapSession.logout()
except :
print ('No attachments were downloaded')
I'm getting the following error:
Traceback (most recent call last):
File "/home/jenns/get_images_from_emails.py", line 12, in <module>
os.mkdir('/var/www/html/attachments')
FileExistsError: [Errno 17] File exists: '/var/www/html/attachments'
I've noticed that python3 is not in #!/usr/bin/env python3 and changed it to #!/usr/bin python3 which didn't produce different results.
I know the gmail side is working because the attachment is moving to the Processed Folder and the email is deleted from my inbox.
I've also used sudo chmod 775 '/var/www/html/attachments' and that didn't resolve the problem. Any ideas as to why the files aren't downloading using the script, get_images_from_emails.py ?
答案1
得分: 1
错误消息告诉您,您试图创建的是一个已经存在的文件夹,而不是一个文件。文件将被覆盖。
这可能是因为在检查其是否存在时没有提供“detach_dir”的完整路径,但在创建它时提供了完整路径。
类似这样的代码应该有效:
attach_dir = '/var/www/html/attachments'
if not os.path.exists(attach_dir):
os.makedirs(attach_dir)
编辑:
关于您的目录结构,我不太确定,但这看起来可能是一个错误...
filePath = os.path.join(detach_dir, '/var/www/html/attachments', fileName)
...因为上面的代码建议您的“detach_dir”将是/var/www/html,因此文件路径将是:/var/www/html/var/www/html/attachments/filename(这个目录很可能不存在)。
英文:
The error message tells you, that the folder you're trying to create already exists, not a file. A file would be overwritten.
This likely happens due to not providing the full path for your "detach_dir" when checking for its existence, but then providing a full path for creating it.
Something like this should work:
attach_dir = '/var/www/html/attachments'
if not os.path.exists(attach_dir):
os.makedirs(attach_dir)
Edit:
And I'm not sure about your directory structure, but this looks like it could be an error ...
filePath = os.path.join(detach_dir, '/var/www/html/attachments', fileName)
... as the above suggests that your "detach_dir" would be /var/www/html, thus the filepath would be: /var/www/html/var/www/html/attachments/filename (a directory that likely doesn't exist)
答案2
得分: 1
感谢 @sm,这是最终有效的脚本!
#!/usr/bin/env python3
# 读取邮件并将附件存储在附件目录中
import email
import getpass, imaplib
import os
import sys
import time
detach_dir = '/var/www/html/attachments'
if not os.path.exists(detach_dir):
os.makedirs(detach_dir)
userName = '在此输入你的gmail账户名'
passwd = '在此输入你的密码'
try:
imapSession = imaplib.IMAP4_SSL('imap.gmail.com',993)
typ, accountDetails = imapSession.login(userName, passwd)
if typ != 'OK':
print ('无法登录!')
raise Exception
imapSession.select('Inbox')
typ, data = imapSession.search(None, 'SUBJECT', '"DASHBOARD"')
if typ != 'OK':
print ('搜索收件箱时出错。')
raise Exception
# 遍历所有邮件
for msgId in data[0].split():
typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
if typ != 'OK':
print ('获取邮件时出错。')
raise Exception
emailBody = messageParts[0][1]
mail = email.message_from_bytes(emailBody)
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join(detach_dir, fileName)
if not os.path.isfile(filePath) :
print (fileName)
print (filePath)
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
# 将邮件移动到已处理部分开始于此
imapSession.select(mailbox='inbox', readonly=False)
resp, items = imapSession.search(None, 'All')
email_ids = items[0].split()
for email in email_ids:
latest_email_id = email
# 移动到已处理
result = imapSession.store(latest_email_id, '+X-GM-LABELS', 'Processed')
if result[0] == 'OK':
# 从收件箱中删除
imapSession.store(latest_email_id, '+FLAGS', '\\Deleted')
# 结束将邮件移动到已处理
imapSession.close()
imapSession.logout()
except :
print ('未下载任何附件')
英文:
Thanks to @sm this was the resulting script that works!
#!/usr/bin/env python3
# read emails and detach attachment in attachments directory
import email
import getpass, imaplib
import os
import sys
import time
detach_dir = '/var/www/html/attachments'
if not os.path.exists(detach_dir):
os.makedirs(detach_dir)
userName = 'enter-your-gmail-account-name-here'
passwd = 'enter-your-password-here'
try:
imapSession = imaplib.IMAP4_SSL('imap.gmail.com',993)
typ, accountDetails = imapSession.login(userName, passwd)
if typ != 'OK':
print ('Not able to sign in!')
raise Exception
imapSession.select('Inbox')
typ, data = imapSession.search(None, 'SUBJECT', '"DASHBOARD"')
if typ != 'OK':
print ('Error searching Inbox.')
raise Exception
# Iterating over all emails
for msgId in data[0].split():
typ, messageParts = imapSession.fetch(msgId, '(RFC822)')
if typ != 'OK':
print ('Error fetching mail.')
raise Exception
emailBody = messageParts[0][1]
mail = email.message_from_bytes(emailBody)
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join(detach_dir, fileName)
if not os.path.isfile(filePath) :
print (fileName)
print (filePath)
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
#MOVING EMAILS TO PROCESSED PART BEGINS HERE
imapSession.select(mailbox='inbox', readonly=False)
resp, items = imapSession.search(None, 'All')
email_ids = items[0].split()
for email in email_ids:
latest_email_id = email
#move to processed
result = imapSession.store(latest_email_id, '+X-GM-LABELS', 'Processed')
if result[0] == 'OK':
#delete from inbox
imapSession.store(latest_email_id, '+FLAGS', '\\Deleted')
#END OF MOVING EMAILS TO PROCESSED
imapSession.close()
imapSession.logout()
except :
print ('No attachments were downloaded')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论