Home > Enterprise >  Why does the executable version of my Python program doesn't execute os.listdir and os.path.isd
Why does the executable version of my Python program doesn't execute os.listdir and os.path.isd

Time:01-02

I have this program that prints a dictionary which has folder names as keys and filenames as values for a given path:

import os
if os.name == 'nt': # Let's add some colors for the lulz
    from ctypes import windll
    k = windll.kernel32
    k.SetConsoleMode(k.GetStdHandle(-11), 7)

# Main method
the_dictionary_list = {}
print('\u001b[43mHi Sailor! I am "SAND-wich", a simple program built by @NoahVerner\033[0m')
print('\n')
time.sleep(2)
def check_path(infile):
    return os.path.exists(infile)    
        
first_entry = input('Tell me the path in which your folders with images are located:')

while True:
    
    if check_path(first_entry) == False:
        print('\n')
        print('This PATH is invalid!')
        first_entry = input('Tell me the RIGHT PATH in which your folders with ONLY images are located:')
        
    elif check_path(first_entry) == True:
        print('\n')
        final_output = first_entry
        break

print('This PATH has the following folders with the following files:')
print('\n')
for name in os.listdir(first_entry):
    if os.path.isdir(name):
        path = os.path.basename(name)
        print(f'\u001b[45m{path}\033[0m')
        list_of_file_contents = os.listdir(path)
        print(f'\033[46m{list_of_file_contents}')
        the_dictionary_list[path] = list_of_file_contents
print('\n')
print('\u001b[43mthe_dictionary_list:\033[0m')
print(the_dictionary_list)
print('\n')

Within a .py file, the code above works as expected without any errors.

Spyder output

After exporting this program as a single executable file using the following sentence on cmd:

pyinstaller --onefile --icon=./SAND-wich_icon.ico SAND-wich.py

I get these folders:

folders

From which the dist folder contains the .exe file that I want.

.exe file

So I run SAND-wich.exe as Admin not without first having my AVG Antivirus deactivated (because it doesn't let that program run correctly neither)

And that executable file DOES RECOGNIZE that the path I'm passing as input IS INDEED A PATH.

However, IT DOES NOT return the desired dictionary with the expected values, it returns an empty one:

empty dictionary

What's causing this problem? Assume that the path I'm passing in both scenarios is the same one and only contain folders which only contain png images, and the os in which the executable file will run is Windows 10.

CodePudding user response:

I figured it out, it was something dumb in the .py code actually

for name in os.listdir(first_entry):
    backslash = "\\"
    if os.path.isdir(first_entry backslash name):
        path = os.path.basename(name)
        print(f'\u001b[45m{path}\033[0m')
        list_of_file_contents = os.listdir(first_entry backslash path)
        print(f'\033[46m{list_of_file_contents}')
        the_dictionary_list[path] = list_of_file_contents
print('\n')
print('\u001b[43mthe_dictionary_list:\033[0m')
print(the_dictionary_list)
print('\n')

Comparison made on solved

  • Related