filter every 24 days file (considering file name) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: filter every 24 days file (considering file name) (/thread-36554.html) |
filter every 24 days file (considering file name) - RolanRoll - Mar-04-2022 Hi I have a folder that contains many eof extension files name I want to sort them in ordinary way with python code (as you can see in my example the name of all my files contain a date like:20190729_20190731 and they are just satellite orbital information files, then select and filtered 1th,24th,47th and.... (index ) of files and delete others because I need every 24 days information files( ex.V20190822T225942_20190824T005942) not all days information .for facility I select and chose these information files from first day I need so the first file is found then I should select 24 days after from first then 47 from first or 24 days after second file and so on. I exactly need to keep my desire files as I said and delete other files in my EOF source folder my desire files are like these S1A_OPER_AUX_POEORB_OPOD_20190819T120911_V20190729T225942_20190731T005942.EOF S1A_OPER_AUX_POEORB_OPOD_20190912T120638_V20190822T225942_20190824T005942.EOF . . . thank you from importlib.metadata import files import pprint items = os.listdir("C:/Users/m/Desktop/EOF") eof_files = [] for item in items: if item.lower().endswith('.eof'): eof_files.append(item) eof_files.sort(key=lambda fname : fname.split('_')[6]) #keeping itemes count=0 for eof_file in eof_files: if fname.split('_')[6]==fname.split('_')[6]+24 print(f"Keeping: [eof_file]") RE: filter every 24 days file (considering file name) - Gribouillis - Mar-04-2022 RollanRoll Wrote:then select and filtered 1th,24th,47th and.... (index ) of files and delete others because I need every 24 days information files( ex.V20190822T225942_20190824T005942) not all days information .for facility I select and chose these information files from first day I need so the first file is found then I should select 24 days after from first then 47 from first or 24 days after second file and so on. These selection criteria are cryptic to me. I hope someone understands them. RE: filter every 24 days file (considering file name) - ibreeden - Mar-04-2022 For calculating with dates you need the datetime module. I assume there is only one file each day and we should only rely on the date and not the time. As I don't have your files I cannot test my program but I think you need something like this. from importlib.metadata import files import pprint from datetime import datetime, timedelta def extract_date(filename: str) -> str: """ Extract the date from the name of a *.EOF file. """ isotimepart = filename.split('_')[6] datepart = isotimepart[1:9] return datepart twentyfourdays = timedelta(days=24) eof_files = [] items = os.listdir("C:/Users/m/Desktop/EOF") for item in items: if item.lower().endswith('.eof'): eof_files.append(item) eof_files.sort(key=lambda fname: fname.split('_')[6]) # Keeping items. # Choose the first file to keep. nextdate = extract_date(eof_files[0]) for eof_file in eof_files: if extract_date(eof_file) == nextdate: print(f"Keeping: {eof_file}") # Create datetime object for date arithmetic. dateobj = datetime.strptime(nextdate, "%Y%m%d") nextobj = dateobj + twentyfourdays nextdate = nextobj.strftime("%Y%m%d")Please let us know if this works for you. RE: filter every 24 days file (considering file name) - RolanRoll - Jun-20-2022 hi again i faced with problem : from importlib.metadata import files ImportError: No module named metadat i use python2.7 [/quote] RE: filter every 24 days file (considering file name) - deanhystad - Jun-20-2022 importlib.metadata was introduced in Python 3.8. It does not exist in Python 2.7. If you look at the python docs you can see this. https://docs.python.org/3/library/importlib.metadata.html Quote:Using importlib.metadata RE: filter every 24 days file (considering file name) - RolanRoll - Jun-23-2022 hi again i have installed python 3.8 as you said and i got result but i want to select keeping files from code and delete other files in folder not just showing the desired files and if it is possible move them to another folder |