ftplib help - delete from server after download results in 0-byte file

Discussion in 'Python' started by Peter A. Schott, Feb 1, 2005.

  1. Got a strange scenario going on here in that I could have sworn this worked
    yesterday. I am issuing binary retrieval calls to an FTP server, writing to a
    file, close the file, then removing the file from the remote site. When I do
    this, I end up with 0 byte files. I was hoping to avoid parsing a list of
    remote and local files and matching them up that way because it would be easier
    to remove on successful retrieve. I'm including some sample code and really
    hoping someone can point out where I'm messing up.

    Thanks.


    import os, ftplib
    DPI = "C:/DestinationFolder/"
    #Actually named different in code, but shorter for reading here.
    #Note - objFTP is already opened - this is not the issue.

    objFTP.cwd("/ksdata_in")

    #I'm open to better ways to do this. Can't use nlst if empty.
    TestDir = objFTP.dir()
    if TestDir <> None:
    FTPRemoteList = objFTP.nlst()

    #If the remote file is type SEM or PGP, do this.
    for filename in [filename for filename in FTPRemoteList if \
    (os.path.splitext(filename)[1] in [".sem",".SEM",".PGP",".pgp"])]:
    try:
    DestinationFile = open(os.path.join(DPI, filename), "wb")
    print DestinationFile
    objFTP.retrbinary("RETR " + filename, DestinationFile.write)
    SQLLogging.LogFTP(filename, CompanyInput, 1, 'ToDrive')
    DestinationFile.close()

    #This is the culprit. When this is active, it writes a 0 byte file locally
    # and deletes the file on the server.
    #objFTP.delete(filename)
    except:
    SQLLogging.LogFTP(filename, CompanyInput, 0, 'ToUs')
    print Exception


    #This is my workaround in the meantime. If I've downloaded the files, get
    #another remote listing and delete and files from remote that are downloaded
    #locally and larger than 1Kbish for PGP files. SEM files are 0 bytes.

    TestDir = objFTP.dir()
    if TestDir <> None:
    FTPRemoteList = objFTP.nlist()
    for filename in FTPRemoteList:
    if os.path.isfile(os.path.join(DownloadedPathInput, filename)) and \
    os.path.splitext(filename)[1] in [".pgp",".PGP"] and \
    os.stat(os.path.join(DownloadedPathInput, filename))[6] > 1000:
    objFTP.delete(filename)
    if os.path.isfile(os.path.join(DownloadedPathInput, filename)) and \
    os.path.splitext(filename)[1] in [".SEM",".sem"]:
    objFTP.delete(filename)
    Peter A. Schott, Feb 1, 2005
    #1
    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. Shawn

    Delete file after download

    Shawn, Nov 26, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    672
    Kevin Spencer
    Nov 26, 2003
  2. Joshua Burvill

    Sending file to print server using ftplib

    Joshua Burvill, May 20, 2004, in forum: Python
    Replies:
    0
    Views:
    314
    Joshua Burvill
    May 20, 2004
  3. Peter A. Schott
    Replies:
    0
    Views:
    870
    Peter A. Schott
    Feb 10, 2005
  4. alex
    Replies:
    3
    Views:
    435
  5. Replies:
    2
    Views:
    472
    Giampaolo Rodola'
    Jun 6, 2008
Loading...

Share This Page