Как мне остановить Python от попытки добавить \ r \ n к имени файла? Я не знаю, как заставить это работать. Я получил это для работы с другим списком вложений, но я просто не вижу ответа на этот. Я впервые работаю с Python
Я пробовал использовать команду R (RAW), но она работает только для имени папки.
Имя файла "AR Invoice_26316. pdf "-> Получение" AR \ r \ n Invoice_26316.pdf "
Не могу изменить имя файла.
Это моя ошибка:
Traceback (most recent call last):
File "test.py", line 43, in <module>
fp = open(att_path, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:/test/AR\r\n Invoice_26316.pdf'
Повторное использование скрипт:
import email, getpass, imaplib, os
detach_dir = "C:/test/"
user = "xxxxxxxxx"
pwd = "xxxxxxxxx"
# connecting to the gmail imap server
m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user,pwd)
m.select("No-Reply") # here you a can choose a mail box like INBOX instead
# use m.list() to get all the mailboxes
resp, items = m.search(None, "ALL")
items = items[0].split() # getting the mails id
for emailid in items:
resp, data = m.fetch(emailid, "(RFC822)") # fetching the mail, "`(RFC822)`" means "get the whole stuff", but you can ask for headers only, etc
email_body = data[0][1] # getting the mail content
mail = email.message_from_string(email_body) # parsing the mail content to get a mail object
#Check if any attachments at all
if mail.get_content_maintype() != 'multipart':
continue
print "["+mail["From"]+"] :" + mail["Subject"]
# we use walk to create a generator so we can iterate on the parts and forget about the recursive headach
for part in mail.walk():
# multipart are just containers, so we skip them
if part.get_content_maintype() == 'multipart':
continue
# is this part an attachment ?
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
att_path = os.path.join(detach_dir, filename)
#Check if its already there
if not os.path.isfile(att_path) :
# finally write the stuff
fp = open(att_path, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()