So i programmed a little GUI to Control some codes i have written recently and i have two tkinter buttons that both have asigned a shutil.move() function. when i click one button, it moves everything to the folder i want it to be in. After clicking the other button it should move the files back to the other folder but it doesnt move them and just gives me the print output, but not the print output in else so its definetly in the if statement
heres my code
def startbot():
global BOT
print("Start bot pressed")
if BOT == "OFF":
print("Bot is off")
for filename in file_stop:
shutil.move(os.path.join(Stop, filename), Post)
BOT = "ON"
print("BOT:", BOT)
else:
print("Bot is already active.")
def stopbot():
global BOT
print("Stop bot Pressed")
if BOT == "ON":
print("Bot is on")
for file_name in file_post:
shutil.move(os.path.join(Post, file_name), Stop)
BOT = "OFF"
print("BOT:", BOT)
else:
print("Bot is already inactive.")
Post is a path and Stop aswell that i create like this
Post = path + "/Post"
Stop = path + "/Stop"
the path variable is selected within the gui and is then saved in a file.
file_post and file_stop are created here
file_post = os.listdir(path + "/Post")
file_stop = os.listdir(path + "/Stop")
os.listdir
returns a static list of the files that are in the directory, not a live view. You won't see the list change after the files have been moved:
>>> file_stop
['myfile1.txt', 'myfile2.txt']
>>> startbot()
...
>>> file_stop
['myfile1.txt', 'myfile2.txt']
Therefore, you should generate the list of files as needed by placing os.listdir
in the functions as part of the for-loop:
def startbot():
...
if BOT == "OFF":
print("Bot is off")
for file_name in os.listdir(Stop):
shutil.move(os.path.join(Stop, file_name), Post)
BOT = "ON" # Move this out of for-loop
print("BOT:", BOT)
...
def stopbot():
...
if BOT == "ON":
print("Bot is on")
for file_name in os.listdir(Post):
shutil.move(os.path.join(Post, file_name), Stop)
BOT = "OFF" # Move this out of for-loop
print("BOT:", BOT)
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.