0

I am trying to iterate over a bunch of .xml files in a directory.

For this purpose, I wrote a python script:

#!/usr/bin/python3.5

import os
import glob

pathToDirectory = '/home/anton/Documents/Repo_from_GitHub/ResiShared/templates/'

for filename in os.listdir(pathToDirectory):
    file = open(pathToDirectory.__add__(filename), "r")
    count = 0
    for line in file:
        if line.__contains__('xmlns="http://xml.juniper.net/xnm/1.1/xnm"') \
                | line.__contains__('xmlns="http://tail-f.com/ned/cisco-ios-xr"') \
                | line.__contains__('xmlns="http://tail-f.com/ned/arista-dcs"'):
            ++count
        elif line.__contains__('tags="replace"'):
            --count
        elif (line.__contains__('</config>') \
                | line.__contains__('</config-template>')) & count > 0:
                    print ('There are ' + str(count) + ' tags="replace" missing in the ' + file.name)

It is working without any bug spotted, but also I got no output from the last "elif", but it definitely should be.

Here is an example of .xml file: xml file example

UPDATE: I do not need any kind of XML parser here, core Python functionality should be enough.

  • 5
    I don't see how this supposedly python3 code can run without any bug spotted, given syntax like `++count` and `--count` – buran Aug 30 '19 at 08:23
  • 1
    Is there any reason why you use binary operators (|, &) rather than logical operators (or, and) ? –  Aug 30 '19 at 08:26
  • Possible duplicate of [How do I parse XML in Python?](https://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python) – manojlds Aug 30 '19 at 08:26
  • 1
    @manojlds this question has absolutely nothing to do with the alleged duplicate other than the fact they both use `python` and `xml` ... – Lord Elrond Aug 30 '19 at 08:49
  • @buran I actually tested that after seeing his code and apparently python completely ignores `++count`, ie you won't get an error – Lord Elrond Aug 30 '19 at 08:55
  • sorry guys, I am a Java dev, and this is my first Python script. That is why I just probably trying to write in Java style :) – Антон Цуркану Aug 30 '19 at 09:01
  • @CalebGoodman, I didn't say it will rasie exceptuion, but that there are bugs in the result even for the if block as well as for the first elif block. I also mention in my answer it will not raise an exception.As @h4z3 explained `++` and `--` is interpreted as applying unary operators `+` ot `-` twice. – buran Aug 30 '19 at 09:01

2 Answers2

3

python does not support ++ and -- operators. Thus when you do ++count and --count the value of count does not change whatsoever and count > 0 is always False.

And note that it will not raise exception, because it's a valid code. ++count is in fact applying unary operator + twice in a row (i.e. +(+count)). Same for -- (-(-count)).

Given the xml sample file you expect that line.__contains__('</config-template>')) & count > 0 is True but it is not.

All that said - I agree with @King'sjester comment and also why you call dunder methods like __contain__() directly? It makes the code less readable and IMHO ugly to say at least. I would take @mannojlds advice to look in more pythonic tools to parse xml files.

>>> line = 'spam'
>>> count = 0
>>> line.__contains__('eggs') & count > 0
False

EDITED to include explanation on unary operators.

buran
  • 13,682
  • 10
  • 36
  • 61
  • 1
    More explanation: `++count` = `count` because those are unary `+`. Same with `--`, those are both unary `-`: `--count` == `-(-count)` == `count`. +Even if `count` wasn't 0, the result is not saved. – h4z3 Aug 30 '19 at 08:49
2

As buran pointed out, there isn't a ++ operator in python, as a result the value of count stays at 0. Changing the following should fix your problem:

++count should be count += 1

--count should be count -= 1

The | symbol is the bitwise operator, instead you should use or.

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80