-1
import tkinter as tk
from ftplib import FTP

pencere = tk.Tk()

pencere.title("Title")
pencere.geometry("800x300")


def deleteAllFiles(ftp):
    for n in ftp.nlst():
        try:
            if n not in ('.','..'):
                print('Working on..'+n)
                try:
                    ftp.delete(n)
                    print('Deleted...'+n)
                except Exception:
                    print(n+' Not deleted, we suspect its a directory, changing to '+n)
                    ftp.cwd(n)
                    deleteAllFiles(ftp)
                    ftp.cwd('..')
                    print('Trying to remove directory ..'+n)
                    ftp.rmd(n)
                    print('Directory, '+n+' Removed')
        except Exception:
            print( 'Trying to remove directory ..'+n)
            ftp.rmd(n)
            print('Directory, '+n+' Removed')

ftp = FTP('***')
username="**"
pwd="**"
ftp.login(username, pwd)
ftp.cwd('htdocs') 
deleteAllFiles(ftp)
print('Done deleting all Files and Directories')

etiket = tk.Label(text="LABEL" ,font = "Verdana 22 bold")
etiket.pack()

button1=tk.Button(pencere, text="Button", command=deleteAllFiles(ftp))
button1.pack()

button2=tk.Button(pencere, text="Quit", command=pencere.quit)
button2.pack()

pencere.mainloop()

This code helps to delete the files in the folder I specified via ftp, but I start the application and apply the command without pressing the button.

running deleteAllFiles(ftp) command without clicking button how can i solve this

current code;

import tkinter as tk
from ftplib import FTP

pencere = tk.Tk()

pencere.title("Title")
pencere.geometry("800x300")

def deleteAllFiles(ftp):
    for n in ftp.nlst():
        try:
            if n not in ('.','..'):
                print('Working on..'+n)
                try:
                    ftp.delete(n)
                    print('Deleted...'+n)
                except Exception:
                    print(n+' Not deleted, we suspect its a directory, changing to '+n)
                    ftp.cwd(n)
                    deleteAllFiles(ftp)
                    ftp.cwd('..')
                    print('Trying to remove directory ..'+n)
                    ftp.rmd(n)
                    print('Directory, '+n+' Removed')
        except Exception:
            print( 'Trying to remove directory ..'+n)
            ftp.rmd(n)
            print('Directory, '+n+' Removed')

ftp = FTP('@@')
username="@@"
pwd="@@"
ftp.login(username, pwd)
ftp.cwd('htdocs') 
#deleteAllFiles(ftp)
print('Done deleting all Files and Directories')

button1=tk.Button(pencere, text="Button", command=lambda: deleteAllFiles(ftp))
button1.pack()

etiket = tk.Label(text="LABEL" ,font = "Verdana 22 bold")
etiket.pack()


button2=tk.Button(pencere, text="Button", command=pencere.quit)
button2.pack()

pencere.mainloop()

this is current code still same command running automatically @Matiiss what can i do please help (I need to write a little more, so I'm writing)

BooWalker
  • 1
  • 3
  • 2
    `command=deleteAllFiles(ftp)` means that you're setting `command` to the returned value of `deleteAllFiles(ftp)`. You're executing `deleteAllFiles` in that line. You have to assign `command` to a function itself, not the return value of a function that you've run. You're never changing `ftp` after setting it, so there's no need for it to be passed in as an argument. You could make it a global or whatever. – Random Davis Aug 13 '21 at 16:04
  • how do i do it? – BooWalker Aug 13 '21 at 16:06
  • 1
    @BooWalker Also at the start of your code just before that print statement you have `deleteAllFiles(ftp)`. – TheLizzard Aug 13 '21 at 16:11
  • @TheLizzard the logic is the same but i tried all of them still the same – BooWalker Aug 13 '21 at 16:14
  • in the current code there is nothing that would directly call that function (besides button which will call it when clicked and the function itself but that doesn't count) so that shouldn't happen, I have no idea why it gets called maybe @TheLizzard can help – Matiiss Aug 13 '21 at 16:59

1 Answers1

2

Change

command=deleteAllFiles(ftp)

to

command=lambda: deleteAllFiles(ftp)

so you're not calling the function right away but wrapping it in a lambda.

AKX
  • 152,115
  • 15
  • 115
  • 172