【发布时间】:2019-03-20 15:52:05
【问题描述】:
我是新来的,对 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__':
感谢您的宝贵时间,任何帮助都会很棒:)
【问题讨论】: