ftp

Discussion in 'Python' started by hawkmoon269, Dec 15, 2004.

  1. hawkmoon269

    hawkmoon269 Guest

    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
     
    hawkmoon269, Dec 15, 2004
    #1
    1. Advertising

  2. hawkmoon269

    Binu K S Guest

    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.

    On 15 Dec 2004 15:49:31 -0800, hawkmoon269 <> wrote:
    > 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
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Binu K S, Dec 16, 2004
    #2
    1. Advertising

  3. hawkmoon269

    hawkmoon269 Guest

    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
     
    hawkmoon269, Dec 17, 2004
    #3
  4. "hawkmoon269" <> wrote:

    > 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>
     
    Fredrik Lundh, Dec 17, 2004
    #4
  5. "hawkmoon269" <> wrote:

    > 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>
     
    Fredrik Lundh, Dec 17, 2004
    #5
  6. hawkmoon269

    hawkmoon269 Guest

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

    hawk
     
    hawkmoon269, Dec 17, 2004
    #6
  7. hawkmoon269

    hawkmoon269 Guest

    That's a good idea. Thanks! :)
     
    hawkmoon269, Dec 17, 2004
    #7
  8. hawkmoon269

    hawkmoon269 Guest

    Just implemented the latter and works beautifully! ...even resolved my
    other issue. Thanks again. :).
     
    hawkmoon269, Dec 17, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. someone

    FTP over SSL vs FTP over SSH

    someone, Apr 24, 2004, in forum: Java
    Replies:
    1
    Views:
    4,635
    iksrazal
    Apr 25, 2004
  2. Replies:
    3
    Views:
    5,740
  3. Nico Grubert
    Replies:
    0
    Views:
    674
    Nico Grubert
    Nov 24, 2005
  4. Nico Grubert
    Replies:
    0
    Views:
    466
    Nico Grubert
    Nov 25, 2005
  5. D. Buck
    Replies:
    2
    Views:
    563
    D. Buck
    Jun 29, 2004
Loading...

Share This Page