Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > TypeError: 'in <string>' requires string as left operand, not Element

Reply
Thread Tools

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

 
 
Victor Hooi
Guest
Posts: n/a
 
      12-10-2012
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))

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
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      12-10-2012
In article <8c78344a-8019-450a-bfdf->,
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))
>
> 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


The first thing I would do is try to figure out if it's happening in the
join() or the open(). Try refactoring this as:

> temp = os.path.join(root, file)
> with open(temp, 'r') as f:


and see which line generates the exception. I'm guessing it's the
join(), but it helps to make sure (so you don't go down some rabbit
hole). Next, I would try to construct a minimal test case. Keep
hacking away code until you get down to the smallest thing which is
produce the problem. I'm guessing something like:

> for root, dirs, files in os.walk('./'):
> for file in files:
> os.path.join(root, file)


might do it. Hopefully that will narrow things down a bit.
 
Reply With Quote
 
 
 
 
Victor Hooi
Guest
Posts: n/a
 
      12-10-2012
Hi,

Ignore me - PEBKAC...lol.

I used "root" both for the os.walk, and also for the root XML element.

Thanks anyhow =).

Cheers,
Victor

On Monday, 10 December 2012 11:52:34 UTC+11, 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))
>
>
>
> 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


 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      12-10-2012
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

 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      12-10-2012
On 2012-12-10 01:19, Dave Angel wrote:
> 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.
>

It looks like Python 3 to me, which doesn't define 'file'.
 
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
How does .rjust() work and why it places characters relative toprevious one, not to first character - placed most to left - or to left sideof screen? crispy Python 6 08-20-2012 12:45 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Help on table align on left of page vs left hanging indent =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?= XML 2 07-16-2007 11:46 AM
Left side of '->' requires a pointer to class; type found was Structure Amit_Basnak C++ 5 02-02-2007 09:25 AM
Left panel on left hand side of desktop on windows xp wish to get rid of Bun Mui Computer Support 1 09-14-2004 03:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57