0
import os
import sys

from lxml import etree
tree = etree.parse("pom.xml")

for elem in tree.findall("{http://maven.apache.org/POM/4.0.0}version"):
   assert elem.text == sys.argv[1]
   elem.text = sys.argv[2]

with open("pom.xml", 'w') as file_handle:
   file_handle.write(etree.tostring(tree, pretty_print=True, encoding='utf8'))

The above Python script can edit the version of pom file which is located in present directory.

But I want to edit all the pom files which are available in all directories through python script .Please find me a solution thankyou.

to find all files Linux command: $find . -name "pom.xml"

Maroun
  • 94,125
  • 30
  • 188
  • 241
revzzz
  • 103
  • 10
  • 2
    This is rather trivial once you know about [`os.walk()`](https://docs.python.org/3/library/os.html#os.walk). – Tim Pietzcker Feb 04 '16 at 08:52
  • "do something" while "finding files recursively"... answered here [http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python](http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python) – Edwin van Mierlo Feb 04 '16 at 09:13
  • Or just `find ... whatever -exec python yourscript.py {} \;` though you'll need to modify your script to take a file name argument from `sys.argv[1]` (or even all of `sys.argv[1:]` in which case you could use `-exec ... \+` instead of `-exec ... \;` if your `find` supports this). – tripleee Feb 04 '16 at 09:16

0 Answers0