spideyclick
spideyclick

Reputation: 182

Python try except else invalid syntax?

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.

Upvotes: 1

Views: 2288

Answers (1)

spideyclick
spideyclick

Reputation: 182

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.

Upvotes: 1

Related Questions