Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Downloading a file form a displayed table

Reply
Thread Tools

Downloading a file form a displayed table

 
 
Joel Goldstick
Guest
Posts: n/a
 
      03-05-2013
On Tue, Mar 5, 2013 at 8:01 AM, Νίκος Γκρ33κ <(E-Mail Removed)> wrote:

> Please help me correct thois code, iam tryign ti for hours and i cant seem
> to get it working....it irritates me....
>


There is no question here. No indication of what isn't working as you
like. No traceback.

>
> path = "/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 = %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
> else:
> #found the page, save primary key and use it to
> issue hit UPDATE
> cID = data[0]
> 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()[1].excepinfo()[2])
> --
> http://mail.python.org/mailman/listinfo/python-list
>




--
Joel Goldstick
http://joelgoldstick.com

 
Reply With Quote
 
 
 
 
Vytas D.
Guest
Posts: n/a
 
      03-05-2013
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=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,
filenames).
....

Vytas


On Tue, Mar 5, 2013 at 1:01 PM, Νίκος Γκρ33κ <(E-Mail Removed)> 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):
> try:
> #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
> else:
> #found the page, save primary key and use it to
> issue hit UPDATE
> cID = data[0]
> 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()[1].excepinfo()[2])
> --
> http://mail.python.org/mailman/listinfo/python-list
>


 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      03-05-2013
On 05/03/2013 09:00, Νίκος Γκρ33κ wrote:
> # ================================================== ================================================== =============
> # display download button for each file and downlaod it on click
> # ================================================== ================================================== =============
> if form.getvalue('show') == 'files':
>
> print ( "<center><a href='http://superhost.gr/files.html'> <img src='/data/images/download.png'></a><br>" )
> print ( "<table border=5 cellpadding=5 bgcolor=blue>" )
>
> path = "/data/files/"
>
> for filename in os.walk(path):
> print '''
> <form method="get" action="%s">
> print( "<tr><td><center> <button type='submit'> %s </button> </td></tr>" ) % filename
> </form>
> '''
>
> sys.exit(0)
>
> ========================
>
> I use the above code to tidplay a filenames table so the user cna download a displayed button style lookign file but its not printing anything for me just an emptry table
>
> '/data/files' has 5 files in it but its not showing any.
> I dont see what iam doign wrong
>


You're doing at least two things wrong.

1) Not reading documentation
2) Not trying the interactive prompt.
3) Not using the print statement/function.
4) Not taking any notice of what people tell you.
5) Not showing what you expect to happen and what actually happens.
6) You're not doing six wrong.

--
Cheers.

Mark Lawrence

 
Reply With Quote
 
33
Guest
Posts: n/a
 
      03-05-2013
, 5 2013 3:38:49 .. UTC+2, Vytas D. :
> 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=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, filenames)..
>
> ...
>
>
>
> Vytas
>
>
>
>
> On Tue, Mar 5, 2013 at 1:01 PM, 33 <(E-Mail Removed)> 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):
>
> * * * * try:
>
> * * * * * * * * #find the needed counter for the page URL
>
> * * * * * * * * cur.execute('''SELECT ID FROM files WHEREURL = %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
>
> * * * * * * * * else:
>
> * * * * * * * * * * * * #found the page, save primary key and use it to issue hit UPDATE
>
> * * * * * * * * * * * * cID = data[0]
>
> * * * * * * * * * * * * 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()[1].excepinfo()[2] )
>
> --
>
> 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 but it also returned coupel things more before the file themselevrs.

thank you a lot for poitning this out to me.
 
Reply With Quote
 
33
Guest
Posts: n/a
 
      03-05-2013
, 5 2013 3:38:49 .. UTC+2, Vytas D. :
> 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=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, filenames)..
>
> ...
>
>
>
> Vytas
>
>
>
>
> On Tue, Mar 5, 2013 at 1:01 PM, 33 <(E-Mail Removed)> 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):
>
> * * * * try:
>
> * * * * * * * * #find the needed counter for the page URL
>
> * * * * * * * * cur.execute('''SELECT ID FROM files WHEREURL = %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
>
> * * * * * * * * else:
>
> * * * * * * * * * * * * #found the page, save primary key and use it to issue hit UPDATE
>
> * * * * * * * * * * * * cID = data[0]
>
> * * * * * * * * * * * * 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()[1].excepinfo()[2] )
>
> --
>
> 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 but it also returned coupel things more before the file themselevrs.

thank you a lot for poitning this out to me.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Japanese Text not displayed on Image Generated by Servlet on winXP, Linux but displayed on Win2000 boney Java 1 12-15-2006 02:24 PM
Table stucture changing when panel is displayed Pavan ASP .Net 3 09-16-2005 11:29 PM
Re: Table Is Displayed Very Strangely Kaypee ASP .Net 1 09-15-2005 09:28 PM
Re: Table Is Displayed Very Strangely Kaypee ASP .Net 1 09-15-2005 05:55 AM
Question: CSV export file displayed in Datagrid or Table - How? VB Programmer ASP .Net 1 07-30-2004 11:14 PM



Advertisments