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

Discussion in 'Python' started by Victor Hooi, Dec 10, 2012.

  1. Victor Hooi

    Victor Hooi Guest

    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
     
    Victor Hooi, Dec 10, 2012
    #1
    1. Advertising

  2. Victor Hooi

    Roy Smith Guest

    In article <>,
    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.
     
    Roy Smith, Dec 10, 2012
    #2
    1. Advertising

  3. Victor Hooi

    Victor Hooi Guest

    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
     
    Victor Hooi, Dec 10, 2012
    #3
  4. Victor Hooi

    Dave Angel Guest

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

    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
     
    Dave Angel, Dec 10, 2012
    #4
  5. Victor Hooi

    MRAB Guest

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

    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'.
     
    MRAB, Dec 10, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Koekenberg
    Replies:
    3
    Views:
    1,386
    Martin Koekenberg
    Aug 26, 2004
  2. Brett C.
    Replies:
    2
    Views:
    2,899
    Martin Koekenberg
    Aug 30, 2004
  3. pc
    Replies:
    2
    Views:
    1,397
    crisgoogle
    Jun 8, 2011
  4. Leo \Equinox\ Gaspard
    Replies:
    1
    Views:
    553
    Alf P. Steinbach /Usenet
    Jul 3, 2011
  5. Leo \Equinox\ Gaspard
    Replies:
    2
    Views:
    413
    James Lothian
    Jul 3, 2011
Loading...

Share This Page