-2

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

cdog1019
  • 9
  • 4

1 Answers1

0

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":
Shadow
  • 8,749
  • 4
  • 47
  • 57
  • Just asking to expand my own knowledge, the != means while not exit correct? – cdog1019 Nov 15 '17 at 22:44
  • While `userinput` does not equal the string `"exit"`, continue to repeat. – Shadow Nov 15 '17 at 22:49
  • where would you place that in the code i have, i placed it like so ` userinput = input()` ` while userinput != "exit":` ` if userinput == 'list':` ` print(names)` and it infinitly loops the list output – cdog1019 Nov 15 '17 at 22:51
  • As I showed in my answer, the while loop should start just **before** requesting user input (and remember to indent everything you wish to repeat) – Shadow Nov 15 '17 at 22:53
  • i tried a couple times to use the code block, both by spaces and in comments help it says use ``'s to no avail. i ended up placing it After userinput () because it would say user input wasn't defined when placed before it – cdog1019 Nov 15 '17 at 22:54
  • So define it before the loop as well. A simple `userinput = ""` would do the trick. – Shadow Nov 15 '17 at 22:57
  • 1
    I thank you for the help and im proud to have figured that out just before i read your comment. success is rare but oh so sweet – cdog1019 Nov 15 '17 at 22:58