我有這個程式可以列印一個字典,該字典將檔案夾名稱作為鍵,檔案名作為給定的值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')
在一個.py
檔案中,上面的代碼按預期作業,沒有任何錯誤。
使用以下陳述句將此程式匯出為單個可執行檔案后cmd
:
pyinstaller --onefile --icon=./SAND-wich_icon.ico SAND-wich.py
我得到這些檔案夾:
從該dist
檔案夾中包含了.exe
我想要的檔案。
所以,我跑SAND-wich.exe
的Admin
不是,而不必首先我 AVG Antivirus
停用(因為它沒有正確地讓這種程式運行既不)
并且該可執行檔案確實認識到 path
我作為輸入傳遞的是 INDEED A PATH。
但是,它不會回傳具有預期值的所需字典,而是回傳一個空字典:
是什么導致了這個問題?假設path
我在兩個場景中傳遞的是相同的一個并且只包含只包含png
影像的檔案夾,并且os
可執行檔案將在其中運行的檔案夾是Windows 10
.
uj5u.com熱心網友回復:
我想通了,它.py
實際上是代碼中的一些愚蠢的東西
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')
在
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/401161.html