Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'error:': 0.05; '[],': 0.07; 'data:': 0.07; 'filename': 0.07; 'filenames': 0.07; 'subject:file': 0.07; 'subject:form': 0.07; 'try:': 0.07; 'python': 0.09; '"but': 0.09; 'filename.': 0.09; 'to:addr:comp.lang.python': 0.09; 'tuple': 0.09; 'cc:addr:python- list': 0.10; 'charset:iso-8859-7': 0.15; '(%s,': 0.16; '(filename,': 0.16; '3-tuple': 0.16; 'bottom-up.': 0.16; 'dirnames,': 0.16; 'hits': 0.16; 'itself),': 0.16; 'os.walk': 0.16; 'reproduce': 0.16; 'tuple,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'code,': 0.18; 'skip:" 30': 0.20; 'import': 0.21; 'errors': 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'values': 0.26; 'cc:addr:gmail.com': 0.27; 'first.': 0.27; 'tree': 0.27; "doesn't": 0.28; 'correct': 0.28; 'actual': 0.28; 'record': 0.28; 'unique,': 0.29; 'yields': 0.29; 'url:mailman': 0.29; '(including': 0.30; "skip:' 10": 0.30; 'folder': 0.30; 'returned': 0.30; 'primary': 0.30; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; 'running': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'skip:s 30': 0.33; 'extract': 0.33; 'handle': 0.33; 'problem': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'wrong': 0.34; 'needed': 0.35; 'path': 0.35; 'pm,': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'url:org': 0.36; 'data.': 0.36; 'url:library': 0.36; 'should': 0.36; 'thank': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'things': 0.38; 'url:docs': 0.38; 'page': 0.38; 'where': 0.40; 'help': 0.40; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'save': 0.61; 'more': 0.63; 'show': 0.63; 'results': 0.65; 'hours': 0.66; 'attention': 0.75; '2013': 0.84; 'cid': 0.84; 'resulted': 0.84; '\xcc\xe1\xf1\xf4\xdf\xef\xf5': 0.84; 'walking': 0.91; 'angel': 0.93 X-Received: by 10.50.53.166 with SMTP id c6mr1127247igp.12.1362494266577; Tue, 05 Mar 2013 06:37:46 -0800 (PST) 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 References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 94.68.127.128 MIME-Version: 1.0 Subject: Re: Downloading a file form a displayed table From: =?ISO-8859-7?B?zd/q7/Igw+rxMzPq?= To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-7 Content-Transfer-Encoding: quoted-printable Cc: =?ISO-8859-7?B?zd/q7/Igw+rxMzPq?= , python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 126 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362494274 news.xs4all.nl 6986 [2001:888:2000:d::a6]:49342 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40531 =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.