ftp

H

hawkmoon269

I would like to write a small ftp script that I could use in place of
DOS. So far I have this --

from ftplib import FTP

server = 'xxx'
username = 'xxx'
password = 'xxx'
file = 'xxx'

ftp = FTP(server)
ftp.login(username, password)
ftp.retrlines('RETR ' + file, open('C:\My Documents\' + file,
'w').write

but this just writes the source files contents into a giant string in
the output file. The source file is comma-delimited with a
fixed-length of 80 chars per line. I need the same format for the
output file. I've tried this --

ftp.retrlines('RETR ' + file, open('C:\My Documents\' + file,
'w').write('\n')

and that gives the correct output format...but it writes the output to
the IDE interpreter command line and not the file. What am I doing
wrong?

hawk
 
B

Binu K S

Try retrbinary instead of retrlines in the original script (the one
without write('\n')).
retrlines fetches the file in ASCII mode and that must be altering the
line terminations.
 
H

hawkmoon269

It turns out that the retrlines method strips of EOL CRLF and \n. My
solution was to create a new method in ftplib that doesn't do this.
I'm assuming that there is a better OOP solution to this, e.g. some
kind of subclassing, but do not have the expertise as yet to implement
that. At any rate, just thought I'd post in case others encountered
the same issue. Incidentally, though, I'm wondering about the slightly
different transfer results. Specifically, when I use DOS, the file
transfers like this --

string, string, string (hidden CRLF)
string, string, string (hidden CRLF)
....

but when I use Python in transfers like this --

string, string, string (hidden CRLF) string, string, string (hidden
CRLF).

If it's not obvious, the source file (sitting on a mainframe somewhere)
is in comma-delimited format.

As it is now, I've coded an Access application to automatically
"import" the file, clean it for my purposes, and "export" it's contents
appropriately etc. But for whatever reason the import wizard inserts a
blank record for each CRLF. When I use DOS and get the format
indicated above, this doesn't occur. Any ideas as to how I could
resolve this? It's not a big deal as I can just have Access refuse to
import blank lines. However, the general procedure does take longer to
execute etc....

hawk
 
F

Fredrik Lundh

hawkmoon269 said:
Specifically, when I use DOS, the file transfers like this --

string, string, string (hidden CRLF)
string, string, string (hidden CRLF)
...

but when I use Python in transfers like this --

string, string, string (hidden CRLF) string, string, string (hidden
CRLF).

what's a "(hidden CRLF)", and how does that differ from a line feed?

</F>
 
F

Fredrik Lundh

hawkmoon269 said:
It turns out that the retrlines method strips of EOL CRLF and \n. My
solution was to create a new method in ftplib that doesn't do this.
I'm assuming that there is a better OOP solution to this, e.g. some
kind of subclassing, but do not have the expertise as yet to implement
that.

reading the documentation might help, somewhat:

retrlines( command[, callback])


Retrieve a file or directory listing in ASCII transfer mode. command should
be an appropriate "RETR" command (see retrbinary()) or a "LIST" command
(usually just the string 'LIST'). The callback function is called for each line,
with the trailing CRLF stripped.

so if you want line endings, just use a callback that adds line endings:

def mycallback(line):
print line

ftp.retrlines("RETR ...", mycallback)

or, perhaps:

file = open("output.txt", "w")

def mycallback(line):
# note: file is a global variable
file.write(line)
file.write("\n")

ftp.retrlines(...)

(since the file is opened in text mode, the write method will automatically
convert "\n" to "\r\n" on windows).

</F>
 
H

hawkmoon269

I just wanted to indicate that a carriage return is present but not
visible.

hawk
 
H

hawkmoon269

Just implemented the latter and works beautifully! ...even resolved my
other issue. Thanks again. :).
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top