我需要帮助解决Python 3.6.5代码的奇怪问题。
错误仅在我的项目执行期间产生,我没有成功在另一个脚本执行期间重新创建它...
我想在__init__
函数中创建一些文件夹到一些类中,使用os.makedirs:
self.working_folder = Preprocessor.prepare_folder(working_folder) # create final working folder
self.previews_folder = Preprocessor.prepare_folder(previews_folder) # create previews folder
此处创建“working_folder”而不是“previews_folder”,没有任何错误。 如果我尝试这段代码:
self.working_folder = Preprocessor.prepare_folder(working_folder) # create final working folder
Preprocessor.prepare_folder(previews_folder) # create previews folder
两个文件夹都已正确创建。
有关“Preprocessor.prepare_folder”的详细信息:
def prepare_folder(folder_path):
print("Path to create: ", folder_path)
while True:
try:
os.makedirs(folder_path)
print("Folder created as: ", folder_path)
return folder_path
except FileExistsError:
if folder_path[-1] in ("/", "\\"):
folder_path = folder_path[:len(folder_path) - 1]
if folder_path[-1].isdigit():
temp = folder_path[-1]
for i in range(len(folder_path) - 2, -1, -1):
try:
if folder_path[i].isdigit():
temp = folder_path[i] + temp
else:
folder_path = folder_path[:i + 1]
break
except IndexError:
raise EnvironmentError
folder_path = folder_path + str(int(temp) + 1)
else:
folder_path += '1'
操作系统:Windows 10 x64系列
回溯:
Path to create: C:\Users\MPuis\Downloads\Tests\classifier_temp
Folder created as: C:\Users\MPuis\Downloads\Tests\classifier_temp
Path to create: C:\Users\MPuis\Downloads\Tests\previews_temp
Folder created as: C:\Users\MPuis\Downloads\Tests\previews_temp
Process finished with exit code 0
带“dir”命令的DOS终端:
Répertoire de C:\Users\MPuis\Downloads\Tests
11/04/2018 23:42 <DIR> .
11/04/2018 23:42 <DIR> ..
11/04/2018 23:42 <DIR> classifier_temp
10/04/2018 12:18 <DIR> Models
10/04/2018 13:12 <DIR> To_sort
0 fichier(s) 0 octets
5 Rép(s) 113 340 448 768 octets libres
最小类样本(正确创建两个文件夹,未成功重新创建问题,但这些是相同的类,值和相关功能,而不是其他不起作用)
""" A script sample of error """
if __name__ == '__main__':
import os
output_folder = os.path.abspath("C:/Users/MPuis/Downloads/Tests/Models")
class Preprocessor:
# ---- Settings ----
default_wfolder = 'classifier_temp'
default_previews = 'previews_temp'
def __init__(self, target_folder, working_folder=default_wfolder, previews_folder=default_previews):
if working_folder == Preprocessor.default_wfolder:
working_folder = os.path.join(os.path.dirname(os.path.abspath(target_folder)), working_folder)
if previews_folder == Preprocessor.default_previews:
previews_folder = os.path.join(os.path.dirname(os.path.abspath(target_folder)), previews_folder)
self.target_folder = target_folder
self.working_folder = Preprocessor.prepare_folder(working_folder) # create final working folder
# Error with this statement
self.previews_folder = Preprocessor.prepare_folder(previews_folder) # create previews folder
@staticmethod
def prepare_folder(folder_path):
print("Path to create: ", folder_path)
while True:
try:
os.makedirs(folder_path)
print("Folder created as: ", folder_path)
return folder_path
except FileExistsError:
print("File exists")
return None
test = Preprocessor(target_folder=output_folder)
print("Script done")
您对这个奇怪的问题有什么建议吗? 谢谢你的帮助!
修改 经过一些测试,我尝试了这段代码:
self.working_folder = Preprocessor.prepare_folder(working_folder) # create final working folder
self.previews_folder = Preprocessor.prepare_folder(previews_folder) # create previews folder
os.makedirs(self.previews_folder)
然后我发现了一个错误:“FileExistsError:[WinError 183]”,但是进入DOS终端的“dir”命令写的结果与先前的测试结果相同......
答案 0 :(得分:0)
比较班级样本和生产类后问题已解决
(按照wwii的建议)
在我的类预处理器中,显然是__del__
函数在退出之前删除了“previews_folder”。
谢谢大家的帮助