import zipfile
import os
import shutil
此压缩方式不带头文件,只有子目录,后面解压要带压缩包名作为外面的大文件夹去解压
import zipfile
import os
import shutil
def zip_yasuo(start_dir):
file_news = start_dir + '.zip'
if not os.path.isfile(file_news):
z = zipfile.ZipFile(file_news, 'w', zipfile.ZIP_DEFLATED)
for dir_path, dir_names, file_names in os.walk(start_dir):
file_path = dir_path.replace(start_dir, '')
file_path = file_path and file_path + os.sep or ''
for filename in file_names:
z.write(os.path.join(dir_path, filename), file_path+filename)
z.close()
if __name__ == "__main__":
oldpath=input("请输入需压缩的文件路径:")
base_path = oldpath
base_path_list = os.listdir(base_path)
for base_path_list_one in base_path_list:
base_path_list_one_dir = os.path.join(base_path,base_path_list_one)
# 子目录
print("准备压缩需要压缩的子目录", base_path_list_one_dir)
if os.path.isdir(base_path_list_one_dir):
zip_yasuo(base_path_list_one_dir)
# shutil.move('F:\\123\\1\\','F:\\123\\2')
import os
import shutil # 文件移动所需的库
# 想要移动文件所在的根目录
rootdir = oldpath
# 获取目录下文件名清单
files = os.listdir(rootdir)
print(files) # 输出文件名单
newpath=input("请输入目标路径:")
for file in files:
if '.zip' in file: # 因为索要移动的文件名均有‘_’,因此利用此判断其是否是所需要移动的文件
full_path = os.path.join(oldpath, file) # 完整的路径
des_path = newpath # 目标路径
shutil.copy(full_path, des_path) # 移动文件到目标路径
print(full_path)
print(des_path)