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


Groups > comp.lang.python > #34534

Re: TypeError: 'in <string>' requires string as left operand, not Element

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
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; 'root': 0.04; 'builtin': 0.07; 'lines.': 0.07; 'root,': 0.07; 'try:': 0.07; 'type,': 0.07; 'block.': 0.09; 'files:': 0.09; 'subject: <': 0.09; 'subject:string': 0.09; 'typeerror:': 0.09; 'cc:addr:python-list': 0.10; 'subject:not': 0.11; 'value.': 0.15; "'r')": 0.16; 'dirs,': 0.16; 'printout': 0.16; 'statements,': 0.16; 'stuff.': 0.16; 'subject: \n ': 0.16; 'variable.': 0.16; "where's": 0.16; '{0}': 0.16; 'string': 0.17; 'wrote:': 0.17; 'element': 0.17; 'file:': 0.22; 'parse': 0.22; "i'd": 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'skip:" 20': 0.26; '(most': 0.27; 'guess': 0.27; 'tree': 0.27; 'run': 0.28; 'types.': 0.29; 'probably': 0.29; "i'm": 0.29; 'error': 0.30; 'asking': 0.32; 'file': 0.32; 'print': 0.32; 'getting': 0.33; 'instead,': 0.33; 'traceback': 0.33; 'problem': 0.33; 'hi,': 0.33; 'along': 0.35; 'pm,': 0.35; 'except': 0.36; 'but': 0.36; 'skip:{ 10': 0.36; 'xml': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'subject:, ': 0.61; 'strange': 0.62; 'different': 0.63; 'show': 0.63; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply- to:no real name:2**0': 0.72; 'received:74.208.4.194': 0.84; 'upset': 0.84; 'victor': 0.84
Date Sun, 09 Dec 2012 20:19:30 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To Victor Hooi <victorhooi@gmail.com>
Subject Re: TypeError: 'in <string>' requires string as left operand, not Element
References <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com>
In-Reply-To <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:2/neJC0DHuJqOqsGqP3Kq/lCgTWpybFLwf8TSCBEbxa 94qpfmosEf4OOSnbegg8E5+aN+MK4pL/ERZjD3a6YjL95XPxu6 Sa7omleZ0DfgAmex5UHn3pTKt/MyXPgdhTZvYd0P36od5pmOx1 MLRudMxNFcBbzay1rvO72owI2eXBzjKL2WjiGOqGv2ATTKFLpP R10uIJGUTtVd2PrrMaYj0UGC3k9DImXeRI9HGRBD/DIdio79kz CBFhbFhswGX7rWh2V34aTXclByzanZw+1cRxF+4b0o3sYkjFEZ yhBsOArPImqKXl5aYCT4QuYIScEvjjxTy9yv229wzOoaFghBA= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
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>
Newsgroups comp.lang.python
Message-ID <mailman.667.1355102395.29569.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1355102395 news.xs4all.nl 6840 [2001:888:2000:d::a6]:46987
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34534

Show key headers only | View raw


On 12/09/2012 07:52 PM, Victor Hooi wrote:
> Hi,
>
> I'm getting a strange error when I try to run the following:
>
>     for root, dirs, files in os.walk('./'):
>         for file in files:
>             if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file:
>                 print(root)
>                 print(file)
>                 with open(os.path.join(root, file), 'r') as f:
>                     print(f.name)
>                     try:
>                         tree = etree.parse(f)
>                         root = tree.getroot()
>                         print(f.name)
>                         print(root.tag)
>                     except xml.parsers.expat.ExpatError as e:
>                         print('Unable to parse file {0} - {1}'.format(f.name, e.message))
>

Where's the printout of the root and file variables?  You're asking
os.path.join to work on them, and it's clearly upset about their types. 
I see print statements, so you clearly were thinking along these lines. 
But you don't show them.  BTW, I'd be using  print(repr(root)) and
print(repr(file)) instead, so you get a better idea of their type and value.

My guess for the problem is that you're trashing 'root' with the
contents of your try block.  Try using a different name for the xml stuff.

> The error is:
>
> Traceback (most recent call last):
>   File "foo.py", line 275, in <module>
>     marketlink_configfiles()
>   File "foo.py", line 83, in bar
>     with open(os.path.join(root, file), 'r') as f:
>   File "C:\Python27\lib\ntpath.py", line 97, in join
>     if path[-1] in "/\\":
> TypeError: 'in <string>' requires string as left operand, not Element
>
> Cheers,
> Victor

Incidentally, 'file' is a builtin type, so it's probably not a good idea
to hide it by using it as your own local variable.

-- 

DaveA

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


Thread

TypeError: 'in <string>' requires string as left operand, not Element Victor Hooi <victorhooi@gmail.com> - 2012-12-09 16:52 -0800
  Re: TypeError: 'in <string>' requires string as left operand, not Element Roy Smith <roy@panix.com> - 2012-12-09 20:08 -0500
  Re: TypeError: 'in <string>' requires string as left operand, not Element Victor Hooi <victorhooi@gmail.com> - 2012-12-09 17:09 -0800
  Re: TypeError: 'in <string>' requires string as left operand, not Element Dave Angel <d@davea.name> - 2012-12-09 20:19 -0500
  Re: TypeError: 'in <string>' requires string as left operand, not Element MRAB <python@mrabarnett.plus.com> - 2012-12-10 02:42 +0000

csiph-web