python - Write text file into a zip without temp files -
in code below, need wright package.inf
directly zip file created @ bottom of script.
import os import zipfile print("note: theme files need inside of 'theme_files' folder.") themename = input("\ntheme name: ") themeauthor = input("\ntheme author: ") themedescription = input("\ntheme description: ") themecontact = input("\nauthor contact: ") otherinfo = input("\nadd custom information? y/n: ") if otherinfo == "y": os.system("cls" if os.name == "nt" else "clear") print("\nenter theme package information below.\n--------------------\n") themeinfo = input() pakname = themename.replace(" ", "_").lower() pakinf = open("package.inf", "w") pakinf.write("theme name: "+ themename +"\n"+"theme author: "+ themeauthor +"\n"+"theme description: "+ themedescription +"\n"+"author contact: "+ themecontact) if otherinfo == "y": pakinf.write("\n"+ themeinfo) pakinf.close() themepak = zipfile.zipfile(pakname +".tpk", "w") dirname, subdirs, files in os.walk("theme_files"): themepak.write(dirname) filename in files: themepak.write(os.path.join(dirname, filename)) themepak.close()
write pakinf
themepak
without creating temporary files.
use io.stringio
create file-like object in memory:
import io import os import zipfile print("note: theme files need inside of 'theme_files' folder.") themename = input("\ntheme name: ") themeauthor = input("\ntheme author: ") themedescription = input("\ntheme description: ") themecontact = input("\nauthor contact: ") otherinfo = input("\nadd custom information? y/n: ") if otherinfo == "y": os.system("cls" if os.name == "nt" else "clear") print("\nenter theme package information below.\n--------------------\n") themeinfo = input() pakname = themename.replace(" ", "_").lower() pakinf = io.stringio() pakinf.write("theme name: "+ themename +"\n"+"theme author: "+ themeauthor +"\n"+"theme description: "+ themedescription +"\n"+"author contact: "+ themecontact) if otherinfo == "y": pakinf.write("\n"+ themeinfo) themepak = zipfile.zipfile(pakname +".tpk", "w") themepak.writestr("package.inf", pakinf.getvalue()) dirname, subdirs, files in os.walk("theme_files"): themepak.write(dirname) filename in files: themepak.write(os.path.join(dirname, filename)) themepak.close()