By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,569 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,569 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 28 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
On Jul 28, 2:27*pm, "Harry" <laiyi...@yahoo.cawrote:
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.
I really appreciate your help. Thanks.
Jul 28 '08 #2

P: n/a
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.

On Jul 28, 11:41*am, MRAB <goo...@mrabarnett.plus.comwrote:
On Jul 28, 2:27*pm, "Harry" <laiyi...@yahoo.cawrote:
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.
I really appreciate your help. Thanks.- Hide quoted text -

- Show quoted text -
Aug 5 '08 #3

P: n/a
Ej wrote:
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>

Aug 5 '08 #4

P: n/a
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!


On Aug 5, 10:37*am, Fredrik Lundh <fred...@pythonware.comwrote:
Ej wrote:
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>- Hide quoted text -

- Show quoted text -
Aug 5 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.