X-Received: by 10.224.175.65 with SMTP id w1mr21037419qaz.7.1362494266654; Tue, 05 Mar 2013 06:37:46 -0800 (PST) X-Received: by 10.50.53.166 with SMTP id c6mr1127247igp.12.1362494266577; Tue, 05 Mar 2013 06:37:46 -0800 (PST) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!dd2no7209444qab.0!news-out.google.com!q17ni604qal.0!nntp.google.com!dd2no7209436qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Tue, 5 Mar 2013 06:37:45 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=94.68.127.128; posting-account=DYJQ-woAAACEPH85Au2BhUVfFTfSfVa4 NNTP-Posting-Host: 94.68.127.128 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Downloading a file form a displayed table From: =?ISO-8859-7?B?zd/q7/Igw+rxMzPq?= Cc: =?ISO-8859-7?B?zd/q7/Igw+rxMzPq?= , python-list@python.org Injection-Date: Tue, 05 Mar 2013 14:37:46 +0000 Content-Type: text/plain; charset=ISO-8859-7 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.python:40530 =D4=E7 =D4=F1=DF=F4=E7, 5 =CC=E1=F1=F4=DF=EF=F5 2013 3:38:49 =EC.=EC. UTC+2= , =EF =F7=F1=DE=F3=F4=E7=F2 Vytas D. =DD=E3=F1=E1=F8=E5: > Hi, >=20 > It is really complicated to reproduce the errors you get by running your = code since it involves database queries. >=20 > Though one thing that really needs your attention is how you handle the r= esults from os.walk(path). >=20 >=20 > Dave Angel told you already that: "But os.walk() doesn't return a filenam= e. It returns a tuple.". >=20 >=20 >=20 > To show where you are wrong I have create the directory structure: >=20 > Folder "folder1" that has files "file4.txt" and "file3.txt" inside. >=20 >=20 >=20 >=20 > In Python 2.6.5: > >>> import os > >>> for filename in os.walk('folder1'): > ...=A0 print(filename) > ...=20 > ('folder1', [], ['file4.txt', 'file3.txt']) >=20 > >>>=20 >=20 >=20 > So your code is treating results from os.walk() incorrectly. You get tupl= e, so extract data you need from it first. In case you don't know how: >=20 >=20 > Print files only (no directories. Adapt code to your needs): >=20 > >>> for result in os.walk('folder1'): > ...=A0=A0=A0 for filename in result[2]: > ...=A0=A0=A0=A0=A0 print(filename) > ...=20 > file4.txt > file3.txt >=20 >=20 >=20 > In http://docs.python.org/2/library/os.html you will find: >=20 > os.walk(top, topdown=3DTrue, onerror=3DNone, followlinks=3DFalse) > Generate the file names in a directory tree by walking the tree either to= p-down or bottom-up. For each directory in the tree rooted at directory top= (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames)= . >=20 > ... >=20 >=20 >=20 > Vytas >=20 >=20 >=20 >=20 > On Tue, Mar 5, 2013 at 1:01 PM, =CD=DF=EA=EF=F2 =C3=EA=F133=EA wrote: >=20 > Please help me correct thois code, iam tryign ti for hours and i cant see= m to get it working....it irritates me.... >=20 >=20 >=20 >=20 > path =3D "/home/nikos/public_html/data/files/" >=20 > for filename in os.walk(path): >=20 > =A0 =A0 =A0 =A0 try: >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #find the needed counter for the page URL >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cur.execute('''SELECT ID FROM files WHERE= URL =3D %s''', (filename,) ) >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 data =3D cur.fetchone() =A0 =A0 =A0 =A0#U= RL is unique, so should only be one >=20 >=20 >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if not data: >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #first time for page; pri= mary key is automatic, hit is defaulted >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cur.execute('''INSERT INT= O files (URL, lastvisit) VALUES (%s, %s)''', (filename, date) ) >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cID =3D cur.lastrowid =A0= =A0 =A0 =A0#get the primary key value of the new record >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else: >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #found the page, save pri= mary key and use it to issue hit UPDATE >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cID =3D data[0] >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cur.execute('''UPDATE fil= es SET hits =3D hits + 1, lastvisit =3D %s WHERE ID =3D %s''', (date, cID) >=20 > =A0 =A0 =A0 =A0 except MySQLdb.Error, e: >=20 > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print ( "Query Error: ", sys.exc_info()[1= ].excepinfo()[2] ) >=20 > -- >=20 > http://mail.python.org/mailman/listinfo/python-list Yes indeed! the problem was at the way os.walk resulted the data. one would think that os.walk would return the actual filenames ina tuple b= ut it also returned coupel things more before the file themselevrs. thank you a lot for poitning this out to me.