Skip to content
Advertisement

How to process files based on their date in Python?

I have two sort of files, xml files and txt files. The files have a date in their name. If the date of the xml file matches the date of a txt file I want to open the txt file do some processing and write the output to a list. After that I want to change the xml file. Multiple xml files can have the same date but the txt file is unique so this means that more then 1 xml file can be linked with a txt file.

Right now I have a problem. my to_csv list contains data of both 20200907 and 20201025. I don’t want it to work like that. I want my to_csv list just do one file (and thus one date) at a time.

JavaScript

Current output:

JavaScript

Desired output:

JavaScript

Advertisement

Answer

You said that you have only one txt file by date and only want to process xml files if they are linked to a txt file. That means that one single loop over txt_dates is enough:

JavaScript

BEWARE: as what you have provided is not a reproducible example I could not test the above code and typos are possible…

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement