我想在python 3.x中使用以下结构创建一个zip存档:
/foldername_YYYYMMDD
/foldername_YYYYMMDD/full/ # contains image files
/foldername_YYYYMMDD/test.json
根据文档,归档文件夹的创建对我来说似乎并不明确。提取存档后,我想要一个已命名的文件夹,其中包含一个完整的子文件夹以及json文件,但我确实得到了:
/full/ # contains image files
/test.json
我的代码:
def cleanup(spider):
name = spider.name
# create zip archive with all images inside
filename = datetime.datetime.now().strftime ("%Y%m%d-%H%M") + '_' + name
imagefolder = 'full'
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "../images"
imagepath = os.path.join(script_dir, rel_path)
# imagepath = '/Users/andy/test_crawl/bid/images'
shutil.make_archive(
'zip/' + filename, # root dir
'zip', # base dir
imagepath,
imagefolder
)
# delete images
shutil.rmtree(imagepath+ '/' + imagefolder)
# add data file to zip archive
filename_zip = filename + '.zip'
zip = zipfile.ZipFile('zip/' + filename_zip,'a')
# path_to_file = '/Users/andy/test_crawl/bid/data/'+ datetime.datetime.now().strftime ("%Y%m%d") + '_' + name + '.json'
path_to_file = script_dir+ '/../data/'+ datetime.datetime.now().strftime ("%Y%m%d") + '_' + name + '.json'
zip.write(path_to_file, os.path.basename(path_to_file)) # add file under same directory without folder names
zip.close()
os.remove(path_to_file)
我需要在哪里添加存档文件夹的名称?