My goal is to create a python's script, which will format/modify the xml file. Path to file & filename are to be given as arguments in command line.
Here is my code below:
import lxml.etree as etree
from argparse import ArgumentParser
import sys
import os
def main():
parser = ArgumentParser()
parser.add_argument('-p', '--path', help="path to file's directory", required=True)
parser.add_argument('-f', '--file', help="file name", required=True)
args = parser.parse_args()
root_dir = sys.argv[1]
file_name = sys.argv[2]
path = sys.argv[1] + sys.argv[2]
for dirpath, dirnames, files in os.walk(root_dir):
for file in files:
if file == file_name:
print(os.path.join(dirpath, file_name))
with open(path, 'r', encoding="utf8") as myfile:
try:
print("DONE")
parser = etree.XMLParser(remove_blank_text = True)
tree = etree.parse(path, parser)
tree.write(path, pretty_print = True)
except IOError:
print("IO Exception Occured")
if __name__ == "__main__":
main()
When I run it from cmd - I have 0 errors, but the file is not formatted, even though I give the filename that doesn't exist - still no errors. So when I run it from cmd - nothing happens.
When I try to debug it in Visual Studio, there is error that 2 arguments need to be given. Can anybody tell me how to fix my code, I have no idea where I have wrong code?