I've made a program that will create a directory of pre made files and folders for various HTML based websites.
Python program works fine when launched form VS Code but as soon as I turn it into a .exe with pyinstaller it says template can not be found "an error expression I made print".
It creates the folder structure but does not create the .css, .html, or .js files into there assigned folders. I think I have narrowed it down to it being something with my file path changing somehow when an .exe is created.
I think the problem lies somewhere in the:
BASE_DIR = Path(__file__).resolve().parent.parent
template_path = os.path.join(BASE_DIR, 'FileRes')
Any and all suggestions would be greatly appreciated.
def createBasicStructure(self, projName):
# create our dir structure
try:
os.makedirs(self.location.get() + '/' + projName)
print("Directory " , projName , " Created ")
except FileExistsError:
print("Directory " , projName , " already exists")
if not os.path.exists(self.location.get() + '/' + projName):
print("Directory " , projName , " no such directory")
else:
os.makedirs(self.location.get() + '/' + projName + '/assets/images')
os.makedirs(self.location.get() + '/' + projName + '/css')
os.makedirs(self.location.get() + '/' + projName + '/scripts')
os.makedirs(self.location.get() + '/' + projName + '/res')
input_files = ['basic.html', 'css/basic.css', 'scripts/basic.js']
BASE_DIR = Path(__file__).resolve().parent.parent
template_path = os.path.join(BASE_DIR, 'FileRes')
for file in input_files:
try:
# Copy sample file
input_file = open(template_path + '/' + file, 'r')
content = input_file.read()
input_file.close()
# Write content
try:
path = self.location.get() + '/' + projName + '/' + file
print(path)
output_file = open(path, 'w')
output_file.write(content)
output_file.close()
# print('\nfile:', path, '\nsuccessfully created')
except:
print('\nFile could not be written')
except:
# new Err
print('\nTemplate file could not be found')
shutilmodule? It can probably perform this copy operation your program is doing via the open/read/close & open/write/close file operations. (To that point, see this article about using thewithstatement for file operations, and context managers in Python)