ftplib and retrbinary or retrlines (losing newline characters in my log files)

Gabriel Genellina gagsl-py at yahoo.com.ar
Thu Jan 25 01:28:32 EST 2007


At Thursday 25/1/2007 03:05, aus stuff wrote:

>Hi am successfully downloading my text files and writing them to 
>local files  with either
>
><ftp://ftp.retrlines('RETR>ftp.retrlines('RETR ' + fl, fileObj.write)"
>
><ftp://ftp.retrbinary('retr/>ftp.retrbinary('RETR ' + fl, fileObj.write)
>
>However all my recieved (log) files have lost thier newline characters?

Both methods are different. From the docs for retrlines: "Retrieve a 
file or directory listing in ASCII transfer mode (...) The callback 
function is called for each line, with the trailing CRLF stripped.". 
The callback should be responsible of writting the missing '\n' then 
(fileObj should have been opened in text mode):

<ftp://ftp.retrlines('RETR>ftp.retrlines('RETR ' + fl, lambda line: 
fileObj.write('%s\n' % line))

Using retrbinary() you get the file in pieces, thay you should write 
in binary mode (just as the original code). The resulting file should 
be identical to the original file on the server. Line endings may be 
different on both platforms (some editors may allow to convert 
between \n, \r\n, \r...). That's why for a log file the ASCII 
transfer mode is better.


-- 
Gabriel Genellina
Softlab SRL 


	

	
		
__________________________________________________ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 




More information about the Python-list mailing list