解压缩符合条件的多个文件并移动到另一个位置 - Python
Unzipping multiple files matching criteria and moving to another location - Python
我是新来的,也是 python 的新手,所以任何帮助都会非常感谢。
我正在尝试减少已完成的代码并自动检查列表中的下一个文件以进行解压和移动,但我发现这很困难,因为我使用了多个参数。
任何人都可以找到更多 simple/automatic 的方法,以便在需要额外的 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()
我是新来的,也是 python 的新手,所以任何帮助都会非常感谢。
我正在尝试减少已完成的代码并自动检查列表中的下一个文件以进行解压和移动,但我发现这很困难,因为我使用了多个参数。
任何人都可以找到更多 simple/automatic 的方法,以便在需要额外的 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()