python批量修改文件名&批量解压缩文件到指定文件夹

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SoftpaseFar/article/details/88970082

直观看效果(就是把第二张图的解压到第三张图)

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

代码展示

import os
import platform

import numpy
import zipfile


# get information of your own system
def identify_platform():
    sys_info = platform.system()
    return str(sys_info)


# get names of zip files
def get_names_of_zip(file_dir):
    sys_type = identify_platform()
    if sys_type == "Windows":
        pipeline = os.popen("dir " + file_dir)
    elif sys_type == "Linux":
        pipeline = os.popen("ls " + file_dir)
    elif sys_type == "Darwin":
        pipeline = os.popen("ls " + file_dir)
    else:
        return

    file_names = numpy.char.split(pipeline.read(), sep='\n')
    # return type:numpy
    return file_names


# rename files in current dir which you point out
def rename_files(file_dir):
    file_names = get_names_of_zip(file_dir).flat
    rows = 1
    sys_type = identify_platform()
    print('\n')
    print('----------------------------begin to rename-------------------------------------')
    for file_name in file_names:
        for item in file_name:
            if str(item) != '':
                if sys_type == "Windows":
                    commend = 'ren  ' + file_dir + str(
                        item) + ' ' + file_dir + str(rows) + '.zip'
                    os.popen(str(commend))
                elif sys_type == "Linux":
                    commend = 'mv -f ' + file_dir + str(
                        item) + ' ' + file_dir + str(rows) + '.zip'
                    os.popen(str(commend))
                elif sys_type == "Darwin":
                    commend = 'mv -f ' + file_dir + str(
                        item) + ' ' + file_dir + str(rows) + '.zip'
                    os.popen(str(commend))
                else:
                    return

                print(commend)
                rows += 1

    print('----------------------------end to rename  -------------------------------------')
    return rows


# unzip file pointed out dir name
def unzip_file(zip_file_dir, unzip_file_dir):
    print('\t\t\t\tlocation:' + unzip_file_dir)
    f = zipfile.ZipFile(zip_file_dir, 'r')
    for file in f.namelist():
        f.extract(file, unzip_file_dir)
    f.close()


# unzip files pointed out dir name
def unzip_files(counts, zip_file_dir, unzip_file_dir):
    print('\n')
    print('----------------------------begin to unzip-------------------------------------')
    counts -= 1
    while counts:
        unzip_file(str(zip_file_dir) + str(counts) + '.zip', unzip_file_dir + '/' + str(counts) + '/')
        counts -= 1
    print('------------------------------end to unzip-------------------------------------')

    return counts


# view of current application
if __name__ == '__main__':
    zip_file_dir = '/Users/simupper/Desktop/temp/'
    unzip_file_dir = '/Users/simupper/Desktop/unzip'
    id_num = rename_files(zip_file_dir)
    result = unzip_files(id_num, zip_file_dir, unzip_file_dir)
    if result == 0:
        print('\n\nformat files is successful')

猜你喜欢

转载自blog.csdn.net/SoftpaseFar/article/details/88970082