Python Forum
filter every 24 days file (considering file name)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
filter every 24 days file (considering file name)
#1
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]")
Reply
#2
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.
menator01 likes this post
Reply
#3
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.
Larz60+ and BashBedlam like this post
Reply
#4
hi again i faced with problem : from importlib.metadata import files
ImportError: No module named metadat
i use python2.7
[/quote]
Reply
#5
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/import...adata.html

Quote:Using importlib.metadata
New in version 3.8.

Changed in version 3.10: importlib.metadata is no longer provisional.
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 943 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 868 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,064 Dec-15-2022, 04:32 PM
Last Post: Larz60+
Photo Making Zip file of a file and Directory Nasir 2 984 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,474 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  How split N days between specified start & end days SriRajesh 2 1,302 May-06-2022, 02:12 PM
Last Post: SriRajesh
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,575 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  How to split file by same values from column from imported CSV file? Paqqno 5 2,705 Mar-24-2022, 05:25 PM
Last Post: Paqqno

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020