Issue
I just made a little program to make some plots with tkinter in python. And I'm trying to make the app to create png file not overlapping the previous one. So like, if it exists, rename it as ~ (1).png kinda thing.
But when I run os.path.isfile
or os.path.exists
, the app just freezes. Like, endless hourglasses turning and saying not responding, you know.
So, this is the function I binded with the button.
def get_information_S():
global nation_list, checked_list, output, year_menu, auto_flag, df
year = year_menu.get()
checked_list.sort()
nations = []
if auto_flag == 0:
for n in range(len(nation_list)):
nations.append(nation_list[n][0].get())
else:
pass
if len(year) == 0:
output_log("연도를 선택해주세요.\n")
elif len(checked_list) == 0:
output_log("hs코드를 선택해주세요.\n")
else:
output_log("\n\n분석 연도 : "+str(year)+"\n분석 품목 : "+str(checked_list)+"\n분석 국가 : "+str(nations)+"\n분석을 실시했습니다.\n")
for x in range(len(checked_list)):
os.makedirs("result", exist_ok=True)
os.makedirs("result\\single", exist_ok=True)
os.makedirs("result\\single\\hs_"+str(checked_list[x]),exist_ok = True)
df_test = df[df.hs6==int(checked_list[x])]
df_test = df_test[df_test.year==int(year)]
file_loc = "result\\single\\hs_"+str(checked_list[x])+"\\hs_"+str(checked_list[x])+"_"+str(year)+'.png'
f = -1
while True:
f+= 1
if os.path.isfile(file_loc) == True:
if f == 0:
file_loc = file_loc.replace('_.png','_('+str(f)+').png')
else:
file_loc = file_loc.replace('('+str(f-1)+')','('+str(f)+')')
continue
else:
break
plain_graph(df_test, file_loc, checked_list[x], nations)
Well, it's my first time dealing with GUI. Is there a way I can use os.path.isfile or os.path.exists? Or any walkaround? Thank you!
Solution
The initial value of file_loc
is something like "result\\single\\hs_xxxx_2021.png"
(assume year
is 2021), then in the first iteration of the while loop, if f == 0
will be evaluated as True
and so
file_loc = file_loc.replace('_.png','_('+str(f)+').png')
will be executed, but nothing will be replaced as _.png
is not found in file_loc
and so file_loc
is kept as initial value and continue
line will proceed to next iteration. As the file_loc
does not change and f
is increased, the else part
file_loc = file_loc.replace('('+str(f-1)+')','('+str(f)+')')
will be executed but again nothing is replaced as the search pattern is not found in file_loc
.
Then same result happens for every iteration followed and make the while loop an infinite loop.
So the mentioned line should be changed to:
file_loc = file_loc.replace('.png','('+str(f)+').png')
Answered By - acw1668
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.