【问题标题】:Python FileNotFoundError despite clearly having found the filePython FileNotFoundError 尽管显然已经找到了文件
【发布时间】:2021-12-16 23:37:15
【问题描述】:

我正在开发一个邮件炸弹程序,该程序可以随机化给定目录中的图像。代码如下所示:

import schedule, time, smtplib, random, os, sys, socket
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart

# Image randomization still under development

def message(subject="Python Notification",
            text="", img="", attachment=None):

    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg.attach(MIMEText(text))

    if type(img) is not list:
        img=[img]
    for one_img in img:
        path = "C:\\Users\\JoeBiden\\Desktop\\PJ\\AMP\\ImgLib"
        files=os.listdir(path)
        randPic=random.choice(files)
        img_data = open(randPic, 'rb').read()
        msg.attach(MIMEImage(img_data,
                             name=os.path.basename(img_data)))

    if attachment is not None:
        if type(attachment) is not list:
            attachment = [attachment]
        for one_attachment in attachment:
            with open(one_attachment, 'rb') as f:
                file = MimeApplication(
                    f.read(),
                    name=os.path.basename(one_attachment)
                )
            file['Content-Disposition'] = f'attachment;\
            filename="{os.path.basename(one_attachment)}"'
            msg-attach(file)
    return msg

def mail():

    smtp=smtplib.SMTP('smtp.gmail.com', 587)
    smtp.ehlo()
    smtp.starttls()
    smtp.login('umomghaey@gmail.com', 'PEEENIS')
    msg = message("Pizza", "Pizza", r"C:\Users\JoeBiden\Desktop\PJ\AMP\ImgLib")
    to = ("cheeseburger@gmail.com")
    smtp.sendmail(from_addr="beesechurger@gmail.com",
                  to_addrs=to, msg=msg.as_string())
    smtp.quit()


schedule.every(2).seconds.do(mail)

while True:
    schedule.run_pending()
    time.sleep(1)

在 CMD 中运行时出现以下错误:

        ret = self.job_func()
  File "C:\Users\JoeBiden\Desktop\PJ\AMP\MailRandomizer.py", line 46, in mail
    msg = message("Pizza", "Pizza", r"C:\Users\JoeBiden\Desktop\PJ\AMP\ImgLib")
  File "C:\Users\JoeBiden\Desktop\PJ\AMP\MailRandomizer.py", line 22, in message
    img_data = open(randPic, 'rb').read()
FileNotFoundError: [Errno 2] No such file or directory: 'RandImageFile.jpg'

对我来说,这个错误似乎很矛盾,因为它清楚地表明它在正确的目录中找到了所需的文件之一。那就是'RandImageFile.jpg'

【问题讨论】:

  • "os.listdir" 只返回没有路径的文件名。

标签: python cmd paradox


【解决方案1】:

os.listdir() 只给你文件名,所以尝试替换

files=os.listdir(path)

类似的东西

files = [os.path.join(path, f) for f in os.listdir(path)]

【讨论】:

  • 不幸的是,这会导致几乎完全相同的错误,只是稍长一些。编辑:或者我应该尝试将它粘贴到我要使用的实际代码中。我认为它奏效了。我现在有一个不同的错误,但这无疑有帮助。谢谢。
  • 新的错误信息是什么?
  • AttributeError: 'bytes' 对象没有属性 'encode'。你的意思是:'解码'?``我现在要去吃午饭,所以很遗憾我现在无法查看这个新错误。如果你有时间,你的意见会很有帮助,但我要等一个小时左右才能回复。再次感谢。
  • 查看新异常的完整回溯非常有用,但猜测name=os.path.basename(img_data) 可能是错误的。你的意思是name=os.path.basename(randPic)吗?
  • 'img_data' 指的是 'img_data = open(randPic, 'rb').read()',因此我假设也是 'randPic'。我会发布回溯,但评论太长了。由于某种原因,该网站在我如何寻求帮助方面非常严格。也就是说,如果你知道一个更好的地方可以向人们询问这些东西,我会喜欢的。
猜你喜欢
  • 2022-12-31
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 2023-01-26
  • 2021-11-30
  • 1970-01-01
相关资源
最近更新 更多