Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34531
| From | Roy Smith <roy@panix.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: TypeError: 'in <string>' requires string as left operand, not Element |
| Date | 2012-12-09 20:08 -0500 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <roy-650A18.20083909122012@news.panix.com> (permalink) |
| References | <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com> |
In article <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com>,
Victor Hooi <victorhooi@gmail.com> 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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