P
Peter A. Schott
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)
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)