ftplib question: how to upload files?

Discussion in 'Python' started by python@sarcastic-horse.com, Sep 11, 2003.

  1. Guest

    Hi:

    I want to write a procedure to automatically upload some files for me, but
    I'm getting stuck. Before I write my own gruesome put() function, I
    wanted to check if there is an easier way.

    Here's what I can do so far:
    >>>import ftplib
    >>>conn = ftplib.FTP('ftp.example.com')
    >>>conn.login(user='userid', passwd='passwd')
    >>>conn.cwd('/var/www/html/incoming')


    What do I do to send files?

    I have three files I want to upload, but I'm not sure what to do. I hoped
    to find a ftplib.put() method, but it looks like ftplib is a low-level
    interface.

    All help is welcome.
    , Sep 11, 2003
    #1
    1. Advertising

  2. On Thu, 11 Sep 2003 14:35:32 -0400 (EDT),
    wrote:


    >What do I do to send files?


    I use this:

    f=file(fileName,'rb')
    conn.storbinary('STOR '+os.path.basename(fileName),f)

    -PU
    Patrick Useldinger, Sep 11, 2003
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    > Hi:
    >
    > I want to write a procedure to automatically upload some files for me, but
    > I'm getting stuck. Before I write my own gruesome put() function, I
    > wanted to check if there is an easier way.
    >
    > Here's what I can do so far:
    > >>>import ftplib
    > >>>conn = ftplib.FTP('ftp.example.com')
    > >>>conn.login(user='userid', passwd='passwd')
    > >>>conn.cwd('/var/www/html/incoming')

    >
    > What do I do to send files?
    >
    > I have three files I want to upload, but I'm not sure what to do. I hoped
    > to find a ftplib.put() method, but it looks like ftplib is a low-level
    > interface.
    >
    > All help is welcome.
    >
    >

    The methods your searching for are storlines (for ascii) or storbinary (for
    non ascii).

    You can simplyfy the process slightly with your own wrapper to those 2
    methods to guess the file mode and call the correct method with the STOR
    command created for you. An example is below.

    def upload(conn, fname):
    (head, tail) = os.path.split(fname)
    command = "STOR " + tail
    fd = open(fname, 'rb')
    temp = fd.read(2048)
    fd.seek(0, 0)
    if temp.find('\0') != -1:
    conn.storbinary(command, fd)
    else:
    conn.storlines(command, fd)
    fd.close()

    Anthony McDonald
    Anthony McDonald, Sep 11, 2003
    #3
  4. Steve Holden Guest

    <> wrote in message
    news:...
    > Hi:
    >
    > I want to write a procedure to automatically upload some files for me, but
    > I'm getting stuck. Before I write my own gruesome put() function, I
    > wanted to check if there is an easier way.
    >
    > Here's what I can do so far:
    > >>>import ftplib
    > >>>conn = ftplib.FTP('ftp.example.com')
    > >>>conn.login(user='userid', passwd='passwd')
    > >>>conn.cwd('/var/www/html/incoming')

    >
    > What do I do to send files?
    >
    > I have three files I want to upload, but I'm not sure what to do. I hoped
    > to find a ftplib.put() method, but it looks like ftplib is a low-level
    > interface.
    >
    > All help is welcome.
    >


    See if the code in

    http://www.holdenweb.com/Python/PDCode/ftpStream.py

    will help. It's supposed to make FTP easier.

    regards
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/pwp/
    Steve Holden, Sep 12, 2003
    #4
    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. Peter A. Schott

    FTPLIB - retry files?

    Peter A. Schott, Jan 17, 2005, in forum: Python
    Replies:
    2
    Views:
    314
    Peter A. Schott
    Jan 19, 2005
  2. Peter A. Schott
    Replies:
    0
    Views:
    259
    Peter A. Schott
    Aug 23, 2005
  3. Thierry Lam

    Renaming files in ftplib

    Thierry Lam, Jan 6, 2006, in forum: Python
    Replies:
    2
    Views:
    578
  4. Peter A. Schott
    Replies:
    1
    Views:
    480
    Martin Franklin
    Jan 18, 2006
  5. Gabriel Genellina
    Replies:
    0
    Views:
    424
    Gabriel Genellina
    Jan 25, 2007
Loading...

Share This Page