Search code examples
pythonxmloperating-systemsubprocesssys

How to call .py on all xml files?


I'm calling a .py inside a python script to read .xml files. My code looks like this :

import os
import sys
import subprocess


subprocess.call(["python", "/home/sky/DBT/test.py", "--host=PC", "--file=/home/sky/data/myfile.xml"])

for a single .xml file it works perfectly. But when i want to run my .py on all my .xmlfiles it does not work. I tried this loop :

for f in ("/home/sky/data/*.xml"):
  subprocess.call(["python", "/home/sky/DBT/test.py", "--host=PC", "--file=f"])

but it does not work for all .xml files in my directory. What is wrong with my code?

Thanks


Solution

  • Try:

    import os
    import sys
    import subprocess
    
    path = "/home/sky/data/"
    for filename in os.listdir(path):   #Iterate Your DIR
        if filename.endswith(".xml"):    #Check if file is XML
            subprocess.call(["python", "/home/sky/DBT/test.py", "--host=PC", "--file=/home/sky/data/{0}".format(filename)])   #Execute Command