Python get recent files FTP [duplicate]
I need to get the newest files/directory on my FTP server (updated today), I've discovered this solution:
def callback(line):
try:
#only use this code if you'll be dealing with that FTP server alone
#look into dateutil module which parses dates with more flexibility
when = datetime.strptime(re.search('[A-z]{3}\s+\d{1,2}\s\d{1,2}:\d{2}', line).group(0), "%b %d %H:%M")
today = datetime.today()开发者_Python百科
if when.day == today.day and when.month == today.month:
pass
print "Updated file"
#####THE CODE HERE#######
except:
print "failed to parse"
return
ftp.retrlines('LIST', callback)
BUT: With this code, I only get multiples "failed to parse" and also multiples "Updated file"-prints. But I need the file/directory name of the file/directory updated today. What is the code to paste in the "#####THE CODE HERE#######"-part to get the directoryname?
Looking at the documentation for the Python ftplib, it looks like the output from retrlines() will be a line where the file name is the last "column".
-rw-r--r-- 1 ftp-usr pdmaint 5305 Mar 20 09:48 INDEX
So a simple split and getting the last field should work. It will however only work if there are no white-space characters in the file/folder name.
name = line.split()[-1]
print(name) # Should be "INDEX"
You might want to employ a more sophisticated parsing if you want to handle names with white-spaces in them.
Use nlst()
to get file names, not retrlines()
.
I would not assume that your filenames have no whitespace.
精彩评论