Search code examples
pythonwindowspython-3.xftpftputil

Python 3.4/Ftputil 3.4 only runs a single command then exits


I'm currently making the base of a personal ftp program, using ftputil 3.4 and python 3.4, ive successfully gotten it to log in and i can run follow up commands in the python interpreter but after a single command it goes back to the main interpreter. as an example if i login, then run list, then it lists once, if i were to try it again it returns the result of typing list in pythons idle shell.

import ftputil

User = input()
Password = input()
ftp = ftputil.FTPHost('ip', User, Password)

names = ftp.listdir(ftp.curdir)
print(names)

userinput = input()
if userinput == 'list':
print(names)

#not yet implemented download function
#if userinput == 'get': 
#   ftp.

I'm looking for a way to keep the program from 'closing' so that i can continue running commands to and from the ftp server


Solution

  • You need to tell python you want to repeat the section of the code. In this case, a while loop probably would do the trick.

    import ftputil
    # ...
    while True:
        userinput = input()
        # ...
    

    This is an infinite loop, so to kill it you'll probably have to press Ctrl+C. Alternatively, if you want to implement an exit command you could do so by doing something like the following;

    while userinput != "exit":