1

So I am trying to set up a small script in Python's IDLE. The IDLE syntax check tells me this code has a syntax error:

from ftplib import FTP
import os
def ftpconnect(address, username, password):
    ftp_connection = 0
    ftp = FTP(address)
    try:
        ftp.login(username, password)
        print(ftp.getwelcome())
        if ftp.getwelcome() == '220 FTP Connected!':
            return 1
    else:
        return 0
print(ftpconnect('10.10.10.xxx', 'xxx', 'xxx'))

The syntax error comes anywhere that I try to get out of the "try" statement, here being the "else:" line. I've looked around and it seems like I have the right syntax...any thoughts?

Thanks! I'm running Python 2, not 3.

3
  • Finally figured it out, see answer... Commented Jul 2, 2014 at 19:45
  • 1
    what exactly are you trying to do with try/else? else makes now sense (and is invalid) unless it is preceded by an except. Commented Jul 2, 2014 at 20:11
  • Looking at this years later, you're totally right! I was totally missing an except statement there, that should have been my first step. Commented Jan 5, 2021 at 16:02

1 Answer 1

1

In addition to the problem with my syntax (missing except statement entirely), my ftp attempt statement was outside of the try block. Since I was not "try"ing it, it failed anyway.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.