On Tue, 2006-12-19 at 07:11 -0800, ray sa wrote:
> Hi
>
> I have just started to learn Python and think it is a superb language.
> I am faced with an issue that I would like some help with. I need to
> fetch files on a daliy basis from a unix machine. I would like to run
> a batch command for this. I would also like to automate this task. The
> files are stored on the unix box; are hourly files with date and time
> as the file name. What I would like to do is log in to the relevant
> directory and fetch the files for that day. I have managed to log in
> to the ftp site and navigate to the directory where the files are. I
> have also used the following command to look for a particular day:
>
> dateMatch = str(localtime()[0])+str(localtime()[1])+str(localtime
> ()[2])
>
> This will give me the date portion of the string to search in the file
> name using:
>
> re.search(dateMatch,filename)
>
> I am now stuck on how to use the
>
> files = listdir(pathName)
>
> to get a list of the files and by using the following code:
>
> for i in files:
> matchFile = search(dateMatch,i)
> if matchFile:
> get the file
>
> What I would like to know is how to get the file using ftplib
> functions. Your expert advice would be very helpful. Please feel free
> to suggest some code and an explanation...
>
> I need help on the following:
>
> -How to get a function to return a list of the files on the directory
session = ftplib.FTP("ftpservername")
session.login() # supply credentials
session.set_pasv(1) # may not be necessary for you
file_list = session.nlst()
> -Using this list I should be able to use the for loop to match the
> date case and
> - fetch the files using the get command. How to use the get command to
> fetch the files in the list prevously where the match was found and
> store this in my local directory?
for f in file_list:
# YOU WILL NEED TO ADD YOUR MATCH LOGIC
session.voidcmd("TYPE I") # binary transfer
size = session.size( f)
outname = "download.tmp"
outfile = file(outname,"wb")
session.retrbinary("RETR " + f, outfile.write)
outfile.close()
mode,ino,dev,nlink,uid,gid,fsize,atime,mtime,ctime = os.stat(outname)
if size == fsize:
shutil.copy(outname, f)
else:
# error handling goes here
Checking the size may be overkill for your needs.
> - Also how to run this python file daily automatically?
Use a cron job to set this up details. Exact details depend on your
flavor of Unix.
>
> Looking forward to you responses..
>
You would almost certainly be better off using SSH (Secure Shell),
public keys, and rsync to do this kind of processing.
> BR
>
> Ray
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> _______________________________________________
> Tutor maillist - [email protected]
> http://mail.python.org/mailman/listinfo/tutor
--
Lloyd Kvam
Venix Corp
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor