0

I need to open multiple XML files for some calculations; my problem is, sometimes these files would open, but the next time they wouldn't, so I cannot trust my code whether it's working right or not:

The piece of code for opening multiple files is:

path_EJ11 = r'C:\Users\Owner\Python\EJ11'

Data_EJ11 = []
for filename in os.listdir(path_EJ11):
    dataa_EJ11 = []
    #cols_EJ11 = []
    with open(filename, 'r') as content:
        tree = ET.parse(content)
        root = tree.getroot()
        for i, child in enumerate(root):
            dataa_EJ11.append([subchild.text for subchild in child])
            #cols_EJ11.append(child.tag)
    Data_EJ11.append(dataa_EJ11)      

but the error I'm getting is:

FileNotFoundError: [Errno 2] No such file or directory: 'corr_1060.xml'

It seems to me the code cannot open the file, Although this file exists in the directory EJ11. Any comment is appreciated; thanks.

AliHZK
  • 1
  • Welcome to Stack Overflow! Please take the [tour]. I've closed your question under an existing one that covers the same issue with answers that explain the concepts of *working directory* and *full path*, which should answer your question. For future reference, please read [ask] for tips like writing a clear title. – wjandrea Jun 04 '22 at 02:33
  • And if you weren't aware, `os.listdir()` returns basenames, not full paths. – wjandrea Jun 04 '22 at 02:35

0 Answers0