: I have been asked to try to program a ftp file stream in python.
: Here's the complete story:
: There's a FTP server with a comma seperated file (csv) on it. A
: device writes log information in to the csv file whenever something
: log-worthy occures. This csv file is around 22 MB and keeps on
: growing. I want to read the latest csv entry that's gotten in to the
: file. So my question is:
: Is it possible to connect to the FTP server, open up a stream to the
: csv file and retrieve the latest data? I have to able to get a
: constant stream and always get the latest csv entry.
: If this is possible wont there be a conflict when the device is
: writing to the .csv file and i'm reading from it?
: If this isn't possible in python, do you have any idea if it is in
: another programming language?
: Hope i explained everything well enough.
: Thanks in advance.
Should be possible. Quick question, though: Do you, or perhaps the person asking for this program, have control over the process that creates this csv?
If so, you and/or they could always change the process to create an empty filename.csv.lock file while it's writing, and remove it when it's finished; then code your client to respect it (i.e., not download the file) if it finds it.
I don't have any example code immediately available for parsing csv's, though there's a nice csv module including with python called, amazingly enough, "csv". :D
On most systems a 'pydoc csv' will give you oodles of information on the module, or you can just do
from an interpreter.
But I do use something like this to pull down data over my lan - since I have gigabit and the files aren't big I don't worry about pulling down the whole thing each time, but if that's not the case for you you'll likely want to look at resuming
to save bandwidth.
from ftplib import FTP
ftp = FTP('hostname')
filename = 'filename.dat'
ls = ftp.retrlines('LIST %s' % filename).split('\n')[-1]
if ls.startswith('226 0'):
ls = ftp.retrlines('LIST %s.lock' % filename).split('\n')[-1]
if not ls.startswith('226 0'):
ftp.retrbinary('RETR %s' % filename, open(filename , 'wb').write)
Hope this helps.