In the code below, I need to wright package.inf
directly into the zip file created at the bottom of the script.
import os
import zipfile
print("Note: All theme files need to be inside of the '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 extra 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")
for dirname, subdirs, files in os.walk("theme_files"):
themePak.write(dirname)
for filename in files:
themePak.write(os.path.join(dirname, filename))
themePak.close()
Write pakInf
into themePak
without creating any temporary files.
Use io.StringIO
to create a file-like object in memory:
import io
import os
import zipfile
print("Note: All theme files need to be inside of the '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 extra 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())
for dirname, subdirs, files in os.walk("theme_files"):
themePak.write(dirname)
for filename in files:
themePak.write(os.path.join(dirname, filename))
themePak.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.