0

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')
1
  • The actual exception traceback/error message would be very useful... also have you looked at using the shutil module? 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 the with statement for file operations, and context managers in Python) Commented Nov 5, 2021 at 15:51

1 Answer 1

0

Had to change

       BASE_DIR = Path(__file__).resolve().parent.parent
                          
                           to 

       BASE_DIR = os.getcwd()

in order for it to read starting from the working directory regardless of py files location or the PC the program is running from.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.