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; 'subject:file': 0.07; 'subject:form': 0.07; 'try:': 0.07; 'python': 0.09; '"but': 0.09; 'filename.': 0.09; 'cc:addr:python- list': 0.10; 'received:74.125.82.44': 0.15; 'charset:iso-8859-7': 0.15; '"but': 0.16; '(%s,': 0.16; '(filename,': 0.16; '3-tuple': 0.16; 'bottom-up.': 0.16; 'dirnames,': 0.16; 'hits': 0.16; 'itself),': 0.16; 'reproduce': 0.16; 'tuple,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'code,': 0.18; 'email addr:gmail.com>': 0.20; 'skip:" 30': 0.20; 'import': 0.21; '>>>': 0.22; 'cc:2**0': 0.23; 'errors': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'skip:[ 10': 0.26; 'values': 0.26; 'first.': 0.27; 'tree': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'correct': 0.28; 'record': 0.28; 'unique,': 0.29; 'yields': 0.29; 'url:mailman': 0.29; 'skip:& 10': 0.29; '(including': 0.30; "skip:' 10": 0.30; 'folder': 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; 'received:74.125.82': 0.33; 'skip:s 30': 0.33; 'extract': 0.33; 'handle': 0.33; 'hi,': 0.33; 'skip:& 20': 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; 'received:74.125': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'url:docs': 0.38; 'page': 0.38; 'where': 0.40; 'help': 0.40; 'url:mail': 0.40; 'your': 0.60; 'save': 0.61; 'show': 0.63; 'results': 0.65; 'hours': 0.66; 'attention': 0.75; '2013': 0.84; 'cid': 0.84; 'walking': 0.91; 'angel': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=+SNUH0hhp+Il9SmhZtgSAngExkW3a553hQWkUjAxYR0=; b=WwMR/a5y1t7FJnOW5Ad7WcAmXGp0VKVfOLIgj/Flf9A92icMq3jv1ScyR/i+PjvEya BNOwxF/S/LQlxnY8HV89FO5/GznOXixPHjdZTT/FCNiRXXnMdeBSBI01KphsBA04UGYc dSl+fGfdNV5IiSaukl5oN6Ba0YuCh0bklXeFjCrkLFpYKRpGaHkGuazWelQfWY2kMCsR MZTgltNSJWyn/XcAsIum3pKuYgrx0aHGPHijqtGwenPzgTiKISk098z0ctoDFrp5jZhS DIII7Jcd2b+Gb03DxpJzOicWDWojqKOMYXSjw7KiNVArpG1oWrqagYYuGtoorMWQ3VPJ ByyA== MIME-Version: 1.0 X-Received: by 10.194.20.72 with SMTP id l8mr39091736wje.36.1362490729898; Tue, 05 Mar 2013 05:38:49 -0800 (PST) In-Reply-To: References: Date: Tue, 5 Mar 2013 13:38:49 +0000 Subject: Re: Downloading a file form a displayed table From: "Vytas D." To: =?ISO-8859-7?B?zd/q7/Igw+rxMzPq?= Content-Type: multipart/alternative; boundary=047d7b5d98adf72bbb04d72d9771 Cc: 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: , Newsgroups: comp.lang.python Message-ID: Lines: 159 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362490737 news.xs4all.nl 6987 [2001:888:2000:d::a6]:33370 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40527 --047d7b5d98adf72bbb04d72d9771 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, It is really complicated to reproduce the errors you get by running your code since it involves database queries. Though one thing that really needs your attention is how you handle the results from os.walk(path). Dave Angel told you already that: "But os.walk() doesn't return a filename. It returns a tuple.". To show where you are wrong I have create the directory structure: Folder "folder1" that has files "file4.txt" and "file3.txt" inside. In Python 2.6.5: >>> import os >>> for filename in os.walk('folder1'): ... print(filename) ... ('folder1', [], ['file4.txt', 'file3.txt']) >>> So your code is treating results from os.walk() incorrectly. You get tuple, so extract data you need from it first. In case you don't know how: Print files only (no directories. Adapt code to your needs): >>> for result in os.walk('folder1'): ... for filename in result[2]: ... print(filename) ... file4.txt file3.txt In http://docs.python.org/2/library/os.html you will find: os.walk(top, topdown=3DTrue, onerror=3DNone, followlinks=3DFalse) Generate the file names in a directory tree by walking the tree either top-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). ... Vytas On Tue, Mar 5, 2013 at 1:01 PM, =CE=9D=CE=AF=CE=BA=CE=BF=CF=82 =CE=93=CE=BA= =CF=8133=CE=BA wrote: > Please help me correct thois code, iam tryign ti for hours and i cant see= m > to get it working....it irritates me.... > > path =3D "/home/nikos/public_html/data/files/" > for filename in os.walk(path): > try: > #find the needed counter for the page URL > cur.execute('''SELECT ID FROM files WHERE URL =3D %s''', > (filename,) ) > data =3D cur.fetchone() #URL is unique, so should > only be one > > if not data: > #first time for page; primary key is automatic, > hit is defaulted > cur.execute('''INSERT INTO files (URL, lastvisit) > VALUES (%s, %s)''', (filename, date) ) > cID =3D cur.lastrowid #get the primary key > value of the new record > else: > #found the page, save primary key and use it to > issue hit UPDATE > cID =3D data[0] > cur.execute('''UPDATE files SET hits =3D hits + 1= , > lastvisit =3D %s WHERE ID =3D %s''', (date, cID) > except MySQLdb.Error, e: > print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2]= ) > -- > http://mail.python.org/mailman/listinfo/python-list > --047d7b5d98adf72bbb04d72d9771 Content-Type: text/html; charset=ISO-8859-7 Content-Transfer-Encoding: quoted-printable
Hi,

It is really complicated to rep= roduce the errors you get by running your code since it involves database q= ueries.

Though one thing that really needs your attention is h= ow you handle the results from os.walk(path).

Dave Angel told you already that: "But os.walk() doesn't retur= n a filename. It returns a tuple.".

To show whe= re you are wrong I have create the directory structure:
Folde= r "folder1" that has files "file4.txt" and "file3.= txt" inside.

In Python 2.6.5:
>>> import os
>= ;>> for filename in os.walk('folder1'):
...=A0 print(filen= ame)
...
('folder1', [], ['file4.txt', 'file3.tx= t'])
>>>

So your code is treating results from os.wa= lk() incorrectly. You get tuple, so extract data you need from it first. In= case you don't know how:

Print files only (no direct= ories. Adapt code to your needs):
>>> for result in os.walk('folder1'):
...=A0=A0=A0 for = filename in result[2]:
...=A0=A0=A0=A0=A0 print(filename)
...
fil= e4.txt
file3.txt

In http://docs.python.org/2/library/os.html = you will find:
os.walk(top, topdown=3DTrue, onerror=3DNone, followlinks=3DFalse)
Genera= te the file names in a directory tree by walking the tree either top-down o= r bottom-up. For each directory in the tree rooted at directory top (includ= ing top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
...

Vytas


On Tue, Mar 5, 2013 at 1:01 PM, = =CD=DF=EA=EF=F2 =C3=EA=F133=EA <nikos.gr33k@gmail.com> w= rote:
Please help me correct thois code, iam tryig= n ti for hours and i cant seem to get it working....it irritates me....

path =3D "/home/nikos/public_html/data/files/"
for filename in os.walk(path):
=A0 =A0 =A0 =A0 try:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #find the needed counter for the page URL =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cur.execute('''SELECT ID FROM f= iles WHERE URL =3D %s''', (filename,) )
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 data =3D cur.fetchone() =A0 =A0 =A0 =A0#URL= is unique, so should only be one

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if not data:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #first time for page; prima= ry key is automatic, hit is defaulted
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cur.execute('''= INSERT INTO files (URL, lastvisit) VALUES (%s, %s)''', (filenam= e, date) )
=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
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #found the page, save prima= ry key and use it to issue hit UPDATE
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cID =3D data[0]
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 cur.execute('''= UPDATE files SET hits =3D hits + 1, lastvisit =3D %s WHERE ID =3D %s'&#= 39;', (date, cID)
=A0 =A0 =A0 =A0 except MySQLdb.Error, e:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print ( "Query Error: ", sys.exc_= info()[1].excepinfo()[2] )
--
http://mail.python.org/mailman/listinfo/python-list

--047d7b5d98adf72bbb04d72d9771--