Search code examples
pythonpython-3.xwhile-looptry-exceptftplib

Try/except block in a while loop for FTP Send/Receive software


I'm trying to build a FTP file send/receive software using python. Here's my code which I built until now.

import ftplib
import getpass

print("FTP File Send-Receive Software")

while True:

    # Get FTP credentials
    try:
        user = input("Username: ")
        print("Password:")
        p = getpass.getpass()
        address = input("Server Address: ")
        session = ftplib.FTP(address,user,p)
        break
    except Exception as error:
        print('ERROR', error)
    else:
        print('Password entered:', session)
        
while True:
    
    # Ask for upload or download
    
    try:

        sorr = input("Send or Receive ? (S/R): ")
        
        while not sorr == "S" or sorr == "R":
        
            sorr = input("Send or Receive ? (S/R): ")
            
    except:

        print('ERROR', error)
        print("Type S for send or R for receive")

    else:

        print(sorr)
        break

# If upload
if sorr == "S":

    while True:

        try:

            ifile = input("Enter file name: ") # Get the file name

        except IOError:

            print("File not accessible")

        except FileNotFoundError:

            print('File not found')

        except:

            print('ERROR', error)

        else:

            pass


    file = open(ifile,'rb')                  # file to send
    session.storbinary('STOR test.txt', file)     # send the file
    file.close()                                    # close file and FTP
    session.quit()
    print("{} uploaded to the server via FTP".format(ifile))

# If download
elif sorr == "R":

    while True:

        try:

            ifile = input("Enter file name: ") # Get the file name

        except IOError:

            print("File not accessible")

        except FileNotFoundError:

            print('File not found')

        except:

            print('ERROR', error)

        else:

            break
    with open(ifile, "wb") as file:
        # use FTP's RETR command to download the file
        ftp.retrbinary(f"RETR {ifile}", file.write)
    ftp.quit()
    print("{} downloded from the server".format(ifile))

When the code executes the while loop for user input to select whether the software starts sending or downloading files, I ask for "S" or "R" letters.

while True:

#Ask for upload or download
    try:

        sorr = input("Send or Receive ? (S/R): ")

        while not sorr == "S" or sorr == "R":

            sorr = input("Send or Receive ? (S/R): ")

    except:

        print('ERROR', error)
        print("Type S for send or R for receive")

    else:

        print(sorr)
        break

When the code proceeds to this part where "sorr" is determined by the user, when I input "S", code executes with no problem. When I input "R", even if I use "while not" here, the code cannot get out of the loop. What is the problem here ? Thank you.


Solution

  • try:
    
            sorr = input("Send or Receive ? (S/R): ")
    
            while not (sorr == "S" or sorr == "R"):
    
                sorr = input("Send or Receive ? (S/R): ")
    

    enclosing the whole thing in round brackets might help