[Tutor] help making my ftp script more pythony
as
pythontutor@infopackaging.com
Mon, 21 Jan 2002 01:06:21 -0500
Hi all,
I am a long time lurker, first time poster. Here is a little python script to =
download the latest virus sigs from CA and run them. It works ok but I know it =
is not very pythony. If you were going to write a simple FTP download what =
would you of done differently. I feel the only functionality that I am missing =
is some user feedback during the file transfer. Which could be done with a =
callback function.
Thanks for any info.
Later,
Troy =20
# Download latest CA Inoculan Personal Edition virus sigs and run.
import ftplib
import win32api
import os
PathList =3D []
def CallBak(DirString):
Path =3D DirString[56:]
# print Path
PathList.append(Path)
return=20
#Connect to host
try:
ftp =3D ftplib.FTP('ftpav.cai.com')
except ftplib.all_errors, e:
print e
#Login
try:
ftp.login() # user anonymous, passwd user@hostname
except ftplib.all_errors, e:
print e
#Change working directory
try:
ftp.cwd('pub/ipe')
except ftplib.all_errors, e:
print e
#Get the directory into PathList
try:
ftp.dir(CallBak)
except ftplib.all_errors, e:
print e
PathList.sort() # sort the list
#Change working directory
try:
ftp.cwd(PathList[-1]) # choose the last item in the list ie. the most =
recient
except ftplib.all_errors, e:
print e
PathList =3D [] # clear the list to use again
#Get the directory into PathList
try:
ftp.dir(CallBak)
except ftplib.all_errors, e:
print e
PathList.sort()
FileName =3D PathList[-1] # choose the last item in the list ie. the most =
recient
TempPath =3D win32api.GetEnvironmentVariable('temp')
if os.path.isfile(TempPath+"\\"+FileName):
try:
os.remove(TempPath+"\\"+FileName) #it's a file, delete it
except:
#probably failed because it is not a normal file
win32api.SetFileAttributes(TempPath+"\\"+FileName, =
win32con.FILE_ATTRIBUTE_NORMAL)
os.remove(TempPath+"\\"+FileName) #it's a file, delete it
print "Downloading " + FileName + " to " + TempPath
F =3D open(TempPath+"\\"+FileName, 'w') =20
ftp.retrbinary('RETR '+FileName, F.write, 1024)
F.close()
print ".....Done....."
# win32api.WinExec(TempPath+"\\"+FileName) # run the downloaded file
ftp.quit()