I needed to get the XML in the folder and parse it, so I tried to get the file path.
Import a library called glob. os is a library required to get the file name.
import glob
import os
Specify all the files in the folder with * (asterisk).
file = glob.glob("C:/Users/user/*")
Since we will get the XML this time, we can get the file path like this.
print(file)
['C:/Users/user/AAAAAA.xml', 'C:/Users/user/BBBBBB.xml']
I want to use a unique file name when exporting to csv, so I also get the file name. First, get AAAAAA.xml from the file path list. Then I used os.path.split to get the filename.
file_A = file[0] //Get the top file path
filename = os.path.split(file_A)[1]
Now you can get the file name.
print(filename)
AAAAAA.xml
Folders are irregular in time and the number of XML files is unpredictable, so you really need to loop. I will post about the loop next time.
Recommended Posts