ftp retrlines with re...

I

isabellknauer

Hey!
Ive been working on an application quite some time now and i wanted to
include something to let the user load a new version. i therefore
tried to include this here:

from ftplib import FTP
import string,re


def handleDownload(block):
processfile.write(block)
print ".",

def load_new_version(old_version):
host='host.bla'
user='usr_ftp_76512'
password='45mhBLl3lXRX.332'
account='usr_ftp_76512'
download=''
directory = 'htdocs'
server = FTP(host)
server.login(user,password,account)
server.cwd(directory)
filelist=server.retrlines('LIST')
#okay this works :logged into the ftpserver,changed dir and now have a
#string that tells the files like this:
#-rw-r--r-- 1 usr_ftp_76512 ftpgroup 14454 Dec 5 14:10
Pict5.2.2.jpg
#-rw-r--r-- 1 usr_ftp_76512 ftpgroup 16174 Dec 5 12:37 Picture
3.jpg
#-rw-r--r-- 1 usr_ftp_76512 ftpgroup 14139 Dec 5 12:38 Picture
6.jpg
#-rw-r--r-- 1 usr_ftp_76512 ftpgroup 42713 Dec 5 12:38 Picture
7.jpg
#226 Transfer complete.
print filelist
files=re.findall(' .{2,20}[.][a-z]{3,3}', filelist,re.I|re.S)
#the dot means every char, because of the flag re.S
# re.I ignores case
#{2,20} this means 2 to twenty of the char that is before it
#heres the problem... if you copy the output and put it into a string
he
#finds all the files with findall, but not inside here... and i dont
get why
#help:(
print files
for file in files:

number=re.findall('pict([0-9])[.]([0-9])[.]?
([0-9]?)',file,re.I)
version=100*int(number[0][0])+ 10*int(number[0][1])+ int(number
[0][2])
if version >old_version:
print version
print old_version
old_version=version
download=file
if download :
processfile = open(download, 'wb')
server.retrbinary('RETR ' + download, handleDownload)
processfile.close()
server.close()
return True
else:
server.close()
return False
load_new_version(201)

#greetz
 
G

Gabriel Genellina

En Fri, 05 Dec 2008 12:51:58 -0200, (e-mail address removed)
Ive been working on an application quite some time now and i wanted to
include something to let the user load a new version. i therefore
tried to include this here:

Too much code, unclear question... please post again, shortening code to
the minimum necesary to show the problem. And tell us *what* is the
problem, what did you expect to happen, what actually happened...
 
P

Peter Otten

filelist=server.retrlines('LIST')

This does not do what you think it does.

gives

"""
Help on method retrlines in module ftplib:

retrlines(self, cmd, callback=None) unbound ftplib.FTP method
Retrieve data in line mode.
The argument is a RETR or LIST command.
The callback function (2nd argument) is called for each line,
with trailing CRLF stripped. This creates a new port for you.
print_line() is the default callback.
"""

I. e. you would need something like

lines = []
server.retrlines("LIST", lines.append)

to store the method's output in a variable instead of printing it to stdout.
But try

files = server.nlist()

instead which gives you a list of filenames.

Peter

PS: Remember to take Gabriel's advice to heart for your next question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top