0

so I have a script with a loop that isn't working and i have no idea why. i need the menu part to loop until i input 0 how could i do this with a while loop? still learning how to use while loops.

#!/usr/bin/env python3
from ftplib import FTP
host     = "localhost"
user     = "chris"
password = "qwerty"
ftp      = FTP(host,user,password)
#
#current working directory of my ftp
#
ftp.cwd("/home/chris")
#
#list of files
#
files = ftp.nlst()
#
#list length to enter as key values
#
list_length = len(files)
#conversoin of list
def Convert(files):
        it = iter(files)
        res_dct = dict(zip(range(1,list_length), it))
        return res_dct
dico_files = Convert(files)
#
#list of files loop
#


for key in dico_files:
        file_list = print(str(key) + ": " + dico_files[key])






#
# menu
#
while selection != 0:
    selection = str(input("what file to choose?"))
    localfile = open(selection, 'wb')
    ftp.retrbinary('RETR ' + selection, localfile.write, 1024)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • In what way is it not working? Is it not looping or are you getting some specific error message when you run this? – Tyberius Dec 17 '20 at 01:39
  • how about thie:https://stackoverflow.com/questions/13180941/how-to-kill-a-while-loop-with-a-keystroke – Zhubei Federer Dec 17 '20 at 01:41
  • @Tyberius it just won't loo after it asks for input and i input anything it says error no such file and just won't loop back to asking me for input again – silentbirdie Dec 17 '20 at 01:49
  • So that's an error. If you give it an invalid file, its going to crash when you try to read it. You should use a `try` block around the parts of the loop after selection and an `except FileNotFoundError` after to catch this exception. – Tyberius Dec 17 '20 at 02:00
  • Please see here: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Mr. T Dec 17 '20 at 09:48
  • @Tyberius sorry had to go to bed and had school today Thanks for the help I will try, lol, this now. – silentbirdie Dec 17 '20 at 20:09
  • @Tyberius `>>> Traceback (most recent call last): File "", line 1, in File "/home/chris/my_scripts/python/IV_connection_ftp.py", line 45 ftp.retrbinary('RETR ' + selection, localfile.write, 1024) ^ SyntaxError: unexpected EOF while parsing` the error is after the last bracket – silentbirdie Dec 17 '20 at 20:47

0 Answers0