【问题标题】:Renaming extracted zip file重命名提取的 zip 文件
【发布时间】:2021-06-23 04:57:33
【问题描述】:

我正在尝试从电子邮件 (.msg) 中提取附件 (zip),然后从 zip 文件中提取唯一的一个文档 (xls)。

提取 xls 后,我想根据 .msg 中的关键字重命名它(即,如果它包含“支付宝”,则在 xls 文件名中附加“_alipay”,否则为“_tng”)

import os
import extract_msg
import fnmatch
import zipfile
import glob

Tk().withdraw()
directory = askdirectory(title='Yo select your folder please')
input_dir = directory + "/"
os.chdir(input_dir)

pwd = '123'
keyword = '*Alipay*'

for email in os.listdir(input_dir):
    if email.endswith('.msg'):
        email_path = os.path.join(input_dir, email)

        if fnmatch.fnmatch(email, keyword):
            trans_type = '_alipay'
        else:
            trans_type = '_tng'

        msg = extract_msg.Message(email)
        msg.save_attachments()
        msg.close()


        for em_zip in glob.glob('*.zip'):
            zip_path = os.path.join(input_dir, em_zip)

                with zipfile.ZipFile(zip_path, 'r') as zf:
                    zf.extractall(pwd=bytes(pwd, 'utf-8'))

                    os.rename(zip_path, os.path.splitext(zip_path)[0] + trans_type + '.xls')

我得到的错误信息是

Traceback(最近一次调用最后一次): 文件“C:\Users\cheeh\Desktop\PyCharmPortable\PycharmProjects\ocrpdf\Alipay.py”,第 71 行,在 os.rename(zip_path, os.path.splitext(zip_path)[0] + trans_type + '.xls') PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用:'C:/Users/cheeh/Desktop/cimb/CDFSB60006039760617520201221.zip' -> 'C:/Users/cheeh/Desktop/cimb /CDFSB60006039760617520201221_alipay.xls'

谢谢

【问题讨论】:

    标签: python for-loop rename zipfile


    【解决方案1】:

    我相信您在文件仍处于打开状态时对其进行重命名,请尝试取消缩进 os.rename 行,以便在重命名文件之前关闭资源。

                with zipfile.ZipFile(zip_path, 'r') as zf:
                    zf.extractall(pwd=bytes(pwd, 'utf-8'))
    
                os.rename(zip_path, os.path.splitext(zip_path)[0] + trans_type + '.xls')
    

    【讨论】:

    • hmm 它重命名 zip 文件而不是提取的 xls
    • 那是因为 os.rename 的第一个参数是你要重命名的文件,你可以改一下
    【解决方案2】:

    我采用手动和更长的方法来实现结果,但如果有任何专业人士可以优化代码,我将不胜感激,因为如果有很多电子邮件,这需要很多时间

    keyword = '*Alipay*'
    os.makedirs(os.path.join(input_dir, '_Alipay'))
    os.makedirs(os.path.join(input_dir, '_TnG'))
    
    for email in glob.iglob('*.msg'):
        email_path = os.path.join(input_dir, email)
    
        if fnmatch.fnmatch(email_path, keyword):
            trans_type = '_Alipay'
        else:
            trans_type = '_TnG'
    
        shutil.move(email_path, os.path.join(input_dir, trans_type))
    
    for emails in glob.iglob('*/*.msg', recursive=True):
        emails_path = os.path.join(input_dir, emails)
    
        if fnmatch.fnmatch(emails_path, keyword):
            trans_type = '_Alipay'
        else:
            trans_type = '_TnG'
    
        msg = extract_msg.Message(emails_path)
        msg.save_attachments(customPath=os.path.join(input_dir, trans_type))
        msg.close()
    
    for zips in glob.glob('*/*.zip', recursive=True):
        zips_path = os.path.join(input_dir, zips)
    
        if fnmatch.fnmatch(zips_path, keyword):
            trans_type = '_Alipay'
        else:
            trans_type = '_TnG'
    
        dst_folder = os.path.join(input_dir, trans_type)
    
        with zipfile.ZipFile(zips, 'r') as zf:
            zf.extractall(path=dst_folder, pwd=bytes(pwd, 'utf-8'))
    
    for xls in glob.glob('*/*.xls', recursive=True):
        xls_path = os.path.join(input_dir, xls)
    
        if fnmatch.fnmatch(xls_path, keyword):
            trans_type = '_Alipay'
        else:
            trans_type = '_TnG'
    
        os.rename(xls_path, os.path.splitext(xls_path)[0] + trans_type + '.xls')
    
    for xls in glob.glob('*/*.xls', recursive=True):
        xls_path = os.path.join(input_dir, xls)
    
        if fnmatch.fnmatch(xls_path, keyword):
            trans_type = '_Alipay'
        else:
            trans_type = '_TnG'
    
        XLS2XLSX(xls_path).to_xlsx(os.path.splitext(xls_path)[0] + '.xlsx')
    
    for xlsx in glob.glob('*/*.xlsx', recursive=True):
        shutil.move(xlsx, input_dir)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 2013-12-14
      • 2022-01-24
      • 2013-03-06
      • 2016-05-19
      • 2018-09-15
      • 1970-01-01
      相关资源
      最近更新 更多