Retrieving file details from FTP server with ftplib (and 'mlsd'?)

I want to track changes to files on our Fanuc robots. To do this I want to get the directory list and file timestamps from the robot’s FTP server. I can get the file list with the following script but no details such as size and timestamp. (Those details are available using WinSCP so the FTP server is able to provide them.)

import ftplib
try:
	ftp.quit()    # Close any connection left open from previous attempt.
except:
	pass

ftp = ftplib.FTP('10.12.29.33', 'myusername', 'mypassword')
files = ftp.nlst("fr:/vision/data")
for file in files:
	print(file)
ftp.quit()

Apparently ftplib’s ‘mlsd’ is what I need for this. But the Script Console reports error:

  File "<input>", line 22, in <module>
AttributeError: FTP instance has no attribute 'mlsd'

How would I figure out if Ignition’s ftplib supports ‘msld’ or not?
Any alternative solutions?

Hmm, looks like mlsd wasn’t added to Python’s ftplib until 3.3.

You might have to get something like Apache Commons Net FTP client into Ignition somehow (module or putting JAR files into lib/core/whatever) and write a bunch of Jython code…

2 Likes

… and we’re all on 2.7. Oh, well. I’ll find another way and avoid “a bunch of Jython code”.

Thanks.

I figured out how to do it fairly simply.

# Retrive a list of files from Fanuc robot FTP server
Import ftplib
import datetime

try:
        ftp.quit()											# Cleanup in case previous script didn't close.
except:
        pass

ftp = ftplib.FTP('10.12.19.19', 'anonymous', '1234')	# Fanuc robot FTP server.
print(ftp.sendcmd('CWD fr:/vision/data'))				# Adapt to suit.
lines = []												# Results
ftp.retrlines('LIST', lines.append)						# Directory listing.
ftp.quit()
# Data retrieved is in format:
# -rw-rw-rw- 1 noone nogroup     178042 nov 18 2021 sf_14_cam1.vd
# ^.........^.........^.........^.........^.........^.........^.........^
# 0         10        20        30        40        50        60        70
for line in lines:
        print(line) 
        print(line[38:49] + " " + line[50:])            # Date and filename.

It doesn’t return the file time but that’s OK in my application.

1 Like