468,720 Members | 1,747 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

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

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)

Jul 18 '05 #1
0 1491

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by python | last post: by
1 post views Thread by John fabiani | last post: by
reply views Thread by Mikael | last post: by
2 posts views Thread by Matija Papec | last post: by
1 post views Thread by Peter A. Schott | last post: by
1 post views Thread by eight02645999 | last post: by
reply views Thread by vedran | last post: by
1 post views Thread by loial | last post: by
1 post views Thread by Laszlo Nagy | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
9 posts views Thread by bryonone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.