problem when getting ftp file

  • Thread starter =?iso-8859-1?q?Francis=20Lavoie?=
  • Start date
?

=?iso-8859-1?q?Francis=20Lavoie?=

I'm new to python and I'm not sure if I use the ftplib
class correctly.

I made a programm to update a pseudo-dynamic site via
ftp. The site is a static html that include files
update by ftp. It create 2 files, and html and a .dat
file, which hold a easier to manipulate data.

first of all, when I start the application, it connect
to ftp, and get the .dat files on the ftp server to
stay up to date. Then modification are done to the
local file, when the application receive a update
request, it take the local file and update it to the
server.

The update work perfectly, but the part I get the file
from ftp cause me some trouble. The client app is run
on windows. To create the file and to read it, I use
open("myfile.dat").write and readlines, there's no
problem, but when I get a new file from ftp, the
return line disapear and readlines take the all the
file as one line.

To write the new file in ftp I use :
storlines("STOR", open("myfile.dat").read)
And to get it I use :
retrlines("RETR", open("myfile.dat").write)


If someone can guide me where to look to find a
solution, it will be appreciated.

Thank you


__________________________________________________________
Lèche-vitrine ou lèche-écran ?
magasinage.yahoo.ca
 
R

Robert M. Emmons

The update work perfectly, but the part I get the file
from ftp cause me some trouble. The client app is run
on windows. To create the file and to read it, I use
open("myfile.dat").write and readlines, there's no
problem, but when I get a new file from ftp, the
return line disapear and readlines take the all the
file as one line.

It sounds like your loosing new lines because of the way your using text
and binary file options. When you open a file with python you can open
it as "r" or "rb" mode in the open statement. For writing it's "w" or
"wb". With the "b" (binary) data is transfered unchanged. Without it,
python will translate newlines \n to \r\n on output and then translate
\r\n to \n on input. This is because different systems use different
textual line terminators -- linux=\n, windows=\r\n, and mac=\r.

I think FTP also has text and binary transfer modes -- too. As a
result, you need to choose all of these combinations carefully.
> To write the new file in ftp I use :
> storlines("STOR", open("myfile.dat").read)
> And to get it I use :
> retrlines("RETR", open("myfile.dat").write)

By the way, the write above looks wrong to me. Don't you need to use
open("myfile.dat","w")?

Rob
 
R

Robert M. Emmons

The update work perfectly, but the part I get the file
from ftp cause me some trouble. The client app is run
on windows. To create the file and to read it, I use
open("myfile.dat").write and readlines, there's no
problem, but when I get a new file from ftp, the
return line disapear and readlines take the all the
file as one line.

It sounds like your loosing new lines because of the way your using text
and binary file options. When you open a file with python you can open
it as "r" or "rb" mode in the open statement. For writing it's "w" or
"wb". With the "b" (binary) data is transfered unchanged. Without it,
python will translate newlines \n to \r\n on output and then translate
\r\n to \n on input. This is because different systems use different
textual line terminators -- linux=\n, windows=\r\n, and mac=\r.

I think FTP also has text and binary transfer modes -- too. As a
result, you need to choose all of these combinations carefully.
> To write the new file in ftp I use :
> storlines("STOR", open("myfile.dat").read)
> And to get it I use :
> retrlines("RETR", open("myfile.dat").write)

By the way, the write above looks wrong to me. Don't you need to use
open("myfile.dat","w")?

Rob
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top