Search code examples
pythonexeos.walk

The os.walk method not work properly after converting to an EXE


I use the os.walk method to get the absolute path of all of the HTML files under the specified path. When I run it as a python script I have no problems, but after converting it to an EXE(Use Pyinstaller), It can't work properly.

Have any matters need attention after converting it to an EXE, or I'm missing something?

import os
import csv
from os import listdir
from os import walk
from os.path import isfile, isdir, join


def get_html_file_path(file_path):
    htmlfile = []
    for root, dirs, files in walk(file_path):
        for f in files:
            # print(f)
            try:
                pass
                if f.split('.')[1] == "html":
                    fullpath = join(root, f)
                    htmlfile.append(fullpath)
            except:
                pass
    return htmlfile


full_path = os.path.realpath(__file__)
file_path = os.path.dirname(full_path)

aaa = get_html_file_path(file_path)

print(aaa)

Solution

  • You can use sys.executable instead for your purpose of obtaining the base path of the main executable:

    import sys
    
    file_path = os.path.dirname(sys.executable)