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

Victor Hooi victorhooi at gmail.com
Mon Dec 10 02:09:02 CET 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




More information about the Python-list mailing list