重命名提取的 zip 文件

Renaming extracted zip file

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

提取 xls 后,我想根据 .msg 中的关键字重命名它(即如果它包含 'Alipay' 然后在 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')

我得到的错误信息是

回溯(最后一次调用): 文件“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'

谢谢

你正在重命名文件,我相信它仍然打开,尝试取消缩进 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')

我采用了一种手动且更长的方式来实现结果,但如果有任何专业人士可以优化代码,我将不胜感激,因为如果有很多电子邮件,这会花费很多时间

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)