Downloading a file form a displayed table
vytasd2013 at gmail.com
Tue Mar 5 14:38:49 CET 2013
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'):
('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:
In http://docs.python.org/2/library/os.html you will find:
os.walk(top, topdown=True, onerror=None, followlinks=False)
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,
On Tue, Mar 5, 2013 at 1:01 PM, Νίκος Γκρ33κ <nikos.gr33k at gmail.com> wrote:
> Please help me correct thois code, iam tryign ti for hours and i cant seem
> to get it working....it irritates me....
> path = "/home/nikos/public_html/data/files/"
> for filename in os.walk(path):
> #find the needed counter for the page URL
> cur.execute('''SELECT ID FROM files WHERE URL = %s''',
> (filename,) )
> data = 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 = cur.lastrowid #get the primary key
> value of the new record
> #found the page, save primary key and use it to
> issue hit UPDATE
> cID = data
> cur.execute('''UPDATE files SET hits = hits + 1,
> lastvisit = %s WHERE ID = %s''', (date, cID)
> except MySQLdb.Error, e:
> print ( "Query Error: ", sys.exc_info().excepinfo() )
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list