python解压文件方法

python解压文件方法,并且输出绝对解压文件的绝对路径

import os
import re
import zipfile


def zip_file():
    url = os.path.abspath(os.path.join(os.path.dirname(__file__)))
    file_list = os.listdir(url)
    files = []

    for file_name in file_list:
        file_url = url + "/" + file_name
        if os.path.splitext(file_name)[1] == '.zip':
            file_zip = zipfile.ZipFile(file_url, 'r')
            for file in file_zip.namelist():
                file_zip.extract(file, url)
                files.append(file)
    script_urls = []
    print(files)
    for file in files:
        file_url = url + "/" + file
        with open(file_url, 'r', encoding="utf-8")as file:
            case_info = file.read()
            case_name = re.findall(r"class (.+)\(", case_info)
            script_url =file.name + "/" + case_name[0]
            script_urls.append(script_url)
    return script_urls


if __name__ == '__main__':
    url = zip_file()
    print(url)

你可能感兴趣的:(python解压文件方法)