Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40531

Re: Downloading a file form a displayed table

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 <nikos.gr33k@gmail.com>
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 <mailman.2884.1362490737.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=94.68.127.128; posting-account=DYJQ-woAAACEPH85Au2BhUVfFTfSfVa4
References <f99ae3d5-6f1f-4dfc-84da-78b758a5147a@googlegroups.com> <ebde8f97-46c4-49c6-a0e1-7056455352d7@googlegroups.com> <mailman.2884.1362490737.2939.python-list@python.org>
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 Νίκος Γκρ33κ <nikos.gr33k@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-7
Content-Transfer-Encoding quoted-printable
Cc Νίκος Γκρ33κ <nikos.gr33k@gmail.com>, 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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.2886.1362494274.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


Τη Τρίτη, 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κ <nikos...@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):
> 
>         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

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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Downloading a file form a displayed table Νίκος Γκρ33κ <nikos.gr33k@gmail.com> - 2013-03-05 01:00 -0800
  Re: Downloading a file form a displayed table Dave Angel <davea@davea.name> - 2013-03-05 04:45 -0500
    Re: Downloading a file form a displayed table Νίκος Γκρ33κ <nikos.gr33k@gmail.com> - 2013-03-05 01:48 -0800
      Re: Downloading a file form a displayed table Dave Angel <davea@davea.name> - 2013-03-05 05:05 -0500
    Re: Downloading a file form a displayed table Νίκος Γκρ33κ <nikos.gr33k@gmail.com> - 2013-03-05 01:48 -0800
  Re: Downloading a file form a displayed table Dave Angel <davea@davea.name> - 2013-03-05 04:51 -0500
    Re: Downloading a file form a displayed table Νίκος Γκρ33κ <nikos.gr33k@gmail.com> - 2013-03-05 02:24 -0800
      Re: Downloading a file form a displayed table Lele Gaifax <lele@metapensiero.it> - 2013-03-05 12:29 +0100
    Re: Downloading a file form a displayed table Νίκος Γκρ33κ <nikos.gr33k@gmail.com> - 2013-03-05 02:24 -0800
  Re: Downloading a file form a displayed table Νίκος Γκρ33κ <nikos.gr33k@gmail.com> - 2013-03-05 05:01 -0800
    Re: Downloading a file form a displayed table Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-05 08:38 -0500
    Re: Downloading a file form a displayed table "Vytas D." <vytasd2013@gmail.com> - 2013-03-05 13:38 +0000
      Re: Downloading a file form a displayed table Νίκος Γκρ33κ <nikos.gr33k@gmail.com> - 2013-03-05 06:37 -0800
      Re: Downloading a file form a displayed table Νίκος Γκρ33κ <nikos.gr33k@gmail.com> - 2013-03-05 06:37 -0800
  Re: Downloading a file form a displayed table Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-03-05 14:04 +0000

csiph-web