I am looking for a specific file within a folder on FTP. There are several folders and my code loops through each folder to find the file. Once, I find the file I would like to download it.
import ftplib
import os, sys, os.path
ftp = ftplib.FTP("xxxxxxxx")
ftp.login("dbaxyz", "password123")
files = []
try:
files = ftp.nlst()
except ftplib.error_perm, resp:
if str(resp) == "550 No files found":
print "No files in this directory"
else:
raise
for f in files:
filess = [],
filess=ftp.nlst(f)
for i in filess:
if len(i)==34 and i[8:11]=="ACO" and i[12:17]=="CCLF9" and i[3:7]=="1263" :
print i
local_filename = os.path.join('C:\\Users\\zubair.zahiruddin\\Desktop\\test', i)
file = open(local_filename, 'wb')
ftp.retrbinary('RETR '+ i, file.write)
file.close()
ftp.close()
#ftp.quit()
However, I get an error: perm file not found.
Traceback (most recent call last):
File "ftp.py", line 28, in <module>
ftp.retrbinary('RETR '+ i, file.write)
File "C:\Python27\lib\ftplib.py", line 414, in retrbinary
conn = self.transfercmd(cmd, rest)
File "C:\Python27\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python27\lib\ftplib.py", line 339, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python27\lib\ftplib.py", line 249, in sendcmd
return self.getresp()
File "C:\Python27\lib\ftplib.py", line 224, in getresp
raise error_perm, resp
ftplib.error_perm: 550 The system cannot find the file specified.
PS C:\Users\zubair.zahiruddin\Desktop\test>