FTP win32 program

Steve Williams stevewilliams at wwc.com
Tue Jul 24 00:16:07 EDT 2001


Acid wrote:

> I am trying to write simple program with Python to connect to an ftp,
> list, download a file(s) etc...
>
> I am running this under Win2K professional with Python 2.1:
>
> [snip]

> I start the Python Gui, then New Window, put the above script in, then
> save, then Run Script.  It returns:
>
>  File "C:/Python21/download.py", line 2
>     from ftplib import FTP
>     ^
> SyntaxError: invalid syntax
>
> What's the trick to get this working under Win32?
>

That caret is in a suspicious place.  How are you putting the script in the
New Window?  Cut and paste?  Check for random leading whitespace.

Anyway, don't use

    'from ftplib import FTP'

even though the code itself and Beasley give that as their example,  because
you'll have trouble catching exceptions.  And you will definitely want to
catch exceptions with ftp.

Use something like

#=============================
import ftplib

#Connect to host
try:
    ftp = ftplib.FTP('x,y,z')
except ftplib.all_errors, e:
    print e
    . . . #punt

#Change working directory
try:
    ftp.sendcmd('CWD /misc'))
except ftplib.all_errors, e:
   print e
   . . . #punt

#List the directory
try:
    ftp.retrlines('LIST')
except ftplib.all_errors, e:
  print e
   . . . #punt
#=========================

instead.

I know the verb 'catch' is not Pythonic, but . . .





More information about the Python-list mailing list