我是新来的,对python来说相当新,所以任何帮助都会非常感谢。
我正在尝试减少我已完成的代码并自动检查列表中的下一个文件以进行解压缩和移动但我发现很难,因为我使用多个参数。
任何人都可以找到更简单/自动的方式,所以如果需要额外的zip文件,我不必更新代码。
mainapp.py
"""main program for selecting files from specific folder based on date criteria and extracting chosen file to another location for pickup by the database
"""
def main():
pass
"""
Library function imports
"""
import zipfile
import os, sys
import shutil
"""enter code here``
Clears old data from folder
"""
# List directory
path = "//var//www/html//"
dirs = os.listdir('folder with zips')
"""
This uses todays date as string, for use to find date in filename
"""
import datetime
now = datetime.datetime.now().isoformat().replace('-','')[:8]
"""
list that is converted into string for each file
"""
matching = [s for s in dirs if now and ".zip" in s]
import deflator
if __name__ == '__main__':
main()
enter code here
这就是问题所在,代码工作得很好,尽可能最好,但问题是这些路径是静态的...我们可以检查这一点,直到没有符合标准
deflator.py
def main():
pass
from mainapp import *
"""counts number of files in dir"""
dircount = len(dirs)
print
"""
date and file path name join
"""
static_path1 = full_path1 = os.path.join('folder join')
full_path1 = os.path.join(static_path1+matching[0])
full_path2 = os.path.join(static_path1+matching[1])
full_path3 = os.path.join(static_path1+matching[2])
full_path4 = os.path.join(static_path1+matching[3])
full_path5 = os.path.join(static_path1+matching[4])
full_path6 = os.path.join(static_path1+matching[5])
"""
Defines folder path, compression type, extract path
"""
static_path2 = ('output folder')
deflate_zip = (zipfile.ZIP_DEFLATED)
zip_ref1 = zipfile.ZipFile(full_path1,'r', deflate_zip)
zip_ref1.extractall(static_path2)
zip_ref2 = zipfile.ZipFile(full_path2,'r', deflate_zip)
zip_ref2.extractall(static_path2)
zip_ref3 = zipfile.ZipFile(full_path3,'r', deflate_zip)
zip_ref3.extractall(static_path2)
zip_ref4 = zipfile.ZipFile(full_path4,'r', deflate_zip)
zip_ref4.extractall(static_path2)
zip_ref5 = zipfile.ZipFile(full_path5,'r', deflate_zip)
zip_ref5.extractall(static_path2)
zip_ref6 = zipfile.ZipFile(full_path6,'r', deflate_zip)
zip_ref6.extractall(static_path2)
if __name__ == '__main__':
谢谢你的时间,任何帮助都会很棒:)
我想通了,但这是我未来过去复杂的人的结果。
任何人都可以帮助用密码解压缩zip。再次感谢 :)
def main():
pass
import os, zipfile,shutil,datetime
import datetime
now = datetime.datetime.now().isoformat().replace('-','')[:8]
deflate_zip = (zipfile.ZIP_DEFLATED)
import os, glob
zip_ref1 = os.chdir('input folder')
for file in glob.glob('*'+(now)+'.zip'):
chicken= zipfile.ZipFile(file,'r', deflate_zip,)
chicken.extractall('output folder')
if __name__ == '__main__':
main()