Python不会覆盖已存在的文件/目录。

huangapple go评论64阅读模式
英文:

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 = &#39;.&#39;
if &#39;attachments&#39; not in os.listdir(detach_dir):
os.mkdir(&#39;/var/www/html/attachments&#39;)
userName = &#39;enter-your-gmail-account-name-here&#39;
passwd = &#39;enter-your-password-here&#39;
try:
imapSession = imaplib.IMAP4_SSL(&#39;imap.gmail.com&#39;,993)
typ, accountDetails = imapSession.login(userName, passwd)
if typ != &#39;OK&#39;:
print (&#39;Not able to sign in!&#39;)
raise Exception
imapSession.select(&#39;Inbox&#39;)
typ, data = imapSession.search(None, &#39;SUBJECT&#39;, &#39;&quot;DASHBOARD&quot;&#39;)
if typ != &#39;OK&#39;:
print (&#39;Error searching Inbox.&#39;)
raise Exception
# Iterating over all emails
for msgId in data[0].split():
typ, messageParts = imapSession.fetch(msgId, &#39;(RFC822)&#39;)
if typ != &#39;OK&#39;:
print (&#39;Error fetching mail.&#39;)
raise Exception
emailBody = messageParts[0][1] 
mail = email.message_from_bytes(emailBody) 
for part in mail.walk():
if part.get_content_maintype() == &#39;multipart&#39;:
continue
if part.get(&#39;Content-Disposition&#39;) is None:
continue
fileName = part.get_filename()
if bool(fileName): 
filePath = os.path.join(detach_dir, &#39;/var/www/html/attachments&#39;, fileName)
if not os.path.isfile(filePath) : 
print (fileName)
print (filePath)
fp = open(filePath, &#39;wb&#39;)
fp.write(part.get_payload(decode=True))
fp.close()
#MOVING EMAILS TO PROCESSED PART BEGINS HERE
imapSession.select(mailbox=&#39;inbox&#39;, readonly=False)
resp, items = imapSession.search(None, &#39;All&#39;)
email_ids = items[0].split()
for email in email_ids:
latest_email_id = email
#move to processed
result = imapSession.store(latest_email_id, &#39;+X-GM-LABELS&#39;, &#39;Processed&#39;)
if result[0] == &#39;OK&#39;:
#delete from inbox
imapSession.store(latest_email_id, &#39;+FLAGS&#39;, &#39;\\Deleted&#39;)
#END OF MOVING EMAILS TO PROCESSED
imapSession.close()
imapSession.logout()
except :
print (&#39;No attachments were downloaded&#39;)

I'm getting the following error:

Traceback (most recent call last):
File &quot;/home/jenns/get_images_from_emails.py&quot;, line 12, in &lt;module&gt;
os.mkdir(&#39;/var/www/html/attachments&#39;)
FileExistsError: [Errno 17] File exists: &#39;/var/www/html/attachments&#39;

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 =  &#39;/var/www/html/attachments&#39;
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, &#39;/var/www/html/attachments&#39;, 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 =  &#39;/var/www/html/attachments&#39;
if not os.path.exists(detach_dir):
os.makedirs(detach_dir)
userName = &#39;enter-your-gmail-account-name-here&#39;
passwd = &#39;enter-your-password-here&#39;
try:
imapSession = imaplib.IMAP4_SSL(&#39;imap.gmail.com&#39;,993)
typ, accountDetails = imapSession.login(userName, passwd)
if typ != &#39;OK&#39;:
print (&#39;Not able to sign in!&#39;)
raise Exception
imapSession.select(&#39;Inbox&#39;)
typ, data = imapSession.search(None, &#39;SUBJECT&#39;, &#39;&quot;DASHBOARD&quot;&#39;)
if typ != &#39;OK&#39;:
print (&#39;Error searching Inbox.&#39;)
raise Exception
# Iterating over all emails
for msgId in data[0].split():
typ, messageParts = imapSession.fetch(msgId, &#39;(RFC822)&#39;)
if typ != &#39;OK&#39;:
print (&#39;Error fetching mail.&#39;)
raise Exception
emailBody = messageParts[0][1] 
mail = email.message_from_bytes(emailBody) 
for part in mail.walk():
if part.get_content_maintype() == &#39;multipart&#39;:
continue
if part.get(&#39;Content-Disposition&#39;) 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, &#39;wb&#39;)
fp.write(part.get_payload(decode=True))
fp.close()
#MOVING EMAILS TO PROCESSED PART BEGINS HERE
imapSession.select(mailbox=&#39;inbox&#39;, readonly=False)
resp, items = imapSession.search(None, &#39;All&#39;)
email_ids = items[0].split()
for email in email_ids:
latest_email_id = email
#move to processed
result = imapSession.store(latest_email_id, &#39;+X-GM-LABELS&#39;, &#39;Processed&#39;)
if result[0] == &#39;OK&#39;:
#delete from inbox
imapSession.store(latest_email_id, &#39;+FLAGS&#39;, &#39;\\Deleted&#39;)
#END OF MOVING EMAILS TO PROCESSED
imapSession.close()
imapSession.logout()
except :
print (&#39;No attachments were downloaded&#39;)

huangapple
  • 本文由 发表于 2023年5月29日 01:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定