Python FTP - NameError: name 'mydpa' is not defined

H

Harry

Hi there. I am trying to download a file(sn.last) from a public FTP
server with the following code:

from ftplib import FTP
ftp=FTP('tgftp.nws.noaa.gov')
ftp.login()
ftp.cwd('SL.us008001/DF.of/DC.radar/DS.81dpr/SI.kbuf')
ftp.retrbinar('RETR sn.last', open(mydpa,'wb').write)
ftp.quit()

but got an error message, which I think is normal:

Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
ftp.retrbinary('RETR sn.last', open(mydpa,'wb').write)
NameError: name 'mydpa' is not defined

I don't know much about python, but just try to use the code to
download data. I don't know where the file will be saved to. Is the
mydata a file name or a folder name? where will it be saved to even
if it's working? Please help be fixed the problem step by step? I am
using 2.5.2 by the way.

I really appreciate your help. Thanks.
 
M

MRAB

Hi there. I am trying to download a file(sn.last) from a public FTP
server with the following code:

from ftplib import FTP
ftp=FTP('tgftp.nws.noaa.gov')
ftp.login()
ftp.cwd('SL.us008001/DF.of/DC.radar/DS.81dpr/SI.kbuf')
ftp.retrbinar('RETR sn.last', open(mydpa,'wb').write)

open(mydpa,'wb') would open the file to which you're saving the data,
but you haven't defined to mydpa, so Python will complain.
ftp.quit()

but got an error message, which I think is normal:

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    ftp.retrbinary('RETR sn.last', open(mydpa,'wb').write)
NameError: name 'mydpa' is not defined
Yep!


I don't know much about python, but just try to use the code to
download data. I don't know where the file will be saved to. Is the
mydata a file name or a folder name? where will it be saved to even
if it's working? Please help be fixed the problem step by step? I am
using 2.5.2 by the way.

You need to tell it where to save the data by defining mydpa.
 
E

Ej

I have a similar problem. I need to download the same file every hour
so it will be nice to be able to rename the downloads with a variable
name.

For example in this case:

from ftplib import FTP
ftp=FTP('tgftp.nws.noaa.gov')
ftp.login()
ftp.cwd('SL.us008001/DF.of/DC.radar/DS.81dpr/SI.kbuf')
ftp.retrbinar('RETR sn.last', open('sn','wb').write)
ftp.quit()

Question: How to achive rename the downloaded files as sn1, sn2,
sn3.... or with a timestamp?
The ftp.rename() is used to rename files on the server, isn't it?I'm
Python shy. Please help.

Any suggestion will be appreciated.
 
F

Fredrik Lundh

Ej said:
I have a similar problem. I need to download the same file every hour
so it will be nice to be able to rename the downloads with a variable
name.

For example in this case:

from ftplib import FTP
ftp=FTP('tgftp.nws.noaa.gov')
ftp.login()
ftp.cwd('SL.us008001/DF.of/DC.radar/DS.81dpr/SI.kbuf')
ftp.retrbinar('RETR sn.last', open('sn','wb').write)
ftp.quit()

Question: How to achive rename the downloaded files as sn1, sn2,
sn3.... or with a timestamp?

you're downloading to the file name you pass to open ("sn" in your
case), so to download to a different file, just pass in another name.

to rename an existing file on the local file system, use os.rename.

</F>
 
E

Ej

Hi Fredrik,

Thanks so much for you quick help!!! I googled the os.rename you gave
me and I found solution to solve my problem.

You made my day!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top