curiosLlama
curiosLlama

Reputation: 25

Return the absolute path to files from different folders

I want to write a function that returns the absolute path to files. The structure of the folders look like this:

2020
    02
      01
        .csv
      02
        .csv
      03
        .csv
      ..
    03
    ..

year -> month -> day - csv file

The function needs to take the last 7 files with x-2 from the current day. Example: if today is 20th of March I need to files from 11th to 17th of march. This is the easy part, but I am stuck now in case where I need to take files from two months. Example today is 3th of march and I need 23th to 29th of Feb or today is 4th of march and I need 24th of Feb to 1st of March. This is my function so far:

currentDay = '05'
currentMonth = '03'
currentYear = '2020'
start_day = int(currentDay) - 9
end_day = int(currentDay) - 2
# print(start_day)
file_path = Path('C:\\Users\\my_files')


paths = []
l = []
for year in os.listdir(file_path):

    if currentMonth == '01' and start_day < 0:
        file_path = os.path.join(file_path, str(int(currentYear)-1))
    else:
        file_path = os.path.join(file_path, currentYear)

    for month in os.listdir(file_path):

        if start_day < 0:
            file_path = os.path.join(file_path, "0"+str(int(currentMonth)-1)+"\\")

        else:
            file_path = os.path.join(file_path, currentMonth)
        for day in range(start_day, end_day):
            paths.append(os.path.join(file_path, '{:02d}'.format(day)+"\\"))

I started writing some if-else conditions, but can't seem to understand how to return paths from when the files are not from current month.

Expected output:

'C:\\Users\\my_files\\2020\\02\\29\\'
'C:\\Users\\my_files\\2020\\03\\01\\'
And so on..

Upvotes: 1

Views: 48

Answers (2)

vishnupal Singh
vishnupal Singh

Reputation: 204

just extended you code .. write a function which return true or false with name leap_year i just used leap_year as a condition . just give it a try :

currentDay = '05'
currentMonth = '03'
currentYear = '2020'
start_day = int(currentDay) - 9
end_day = int(currentDay) - 1
# print(start_day)
file_path = Path('C:\\Users\\my_files'

paths = []
l = []
for year in os.listdir(file_path):

if currentMonth == '01' and start_day < 0:
    file_path = os.path.join(file_path, str(int(currentYear)-1))
else:
    file_path = os.path.join(file_path, currentYear)

for month in os.listdir(file_path):

    if start_day < 0 and currentMonth in ['10', '11','12']:
        file_path = os.path.join(file_path, "0"+str(int(currentMonth)-1)+"\\")
    elif start_day < 0:
         file_path = os.path.join(file_path, str(int(currentMonth)-1)+"\\")
    else:
        file_path = os.path.join(file_path, currentMonth)
    if int(currentDay) < 9 :
         if currentMonth == '03':
            max_days = 29 if leap_year else 28                             
            for day in range(1, end_day): 
                paths.append(os.path.join(file_path, '{:02d}'.format(day)+"\\"))
            for days in range (max_days, max_days-9-int(currentDay)-1, -1):
                paths.append(os.path.join(file_path, '{:02d}'.format(day)+"\\"))  
         if currentMonth in ['01','02', '04', '06', '08', '09', '11']:
            max_days = 31
            for day in range(1, end_day): 
                paths.append(os.path.join(file_path, '{:02d}'.format(day)+"\\"))
            for days in range (max_days, max_days-9-int(currentDay)-1, -1):
                paths.append(os.path.join(file_path, '{:02d}'.format(day)+"\\")) 
         if currentMonth in ['05', '07', '10','12']:
            max_days = 30
            for day in range(1, end_day): 
                paths.append(os.path.join(file_path, '{:02d}'.format(day)+"\\"))
            for days in range (max_days, max_days-9-int(currentDay)-1, -1):
                paths.append(os.path.join(file_path, '{:02d}'.format(day)+"\\")) 
    else:                              
        for day in range(start_day, end_day):
            paths.append(os.path.join(file_path, '{:02d}'.format(day)+"\\"))

Upvotes: 0

pastaleg
pastaleg

Reputation: 1838

Great question. Date logic can be very hard to implement yourself because there may be many exceptions that complicate the logic. Python has a module called datetime that makes this process easier because someone else has already implemented how many days there are in a month, year, and potential leap year.

With datetime, you can get the day pretty easily

import datetime
a_week_ago = datetime.datetime.now() - datetime.timedelta(days=7)

Now, you need to convert the date object to a string that fits your file structure. Many options exists here, this is a suggestion:

import datetime

new_date = datetime.datetime.now() - datetime.timedelta(days=7)
prefix = r"C:\\Users\\my_files\\"
new = prefix + new_date.strftime(r"%Y\\%m\\%d\\")
print(new)

Then you can put that in a loop and modify the days so that it is not 7, but looping backwards.

import datetime

number_of_days = 7
file_path = Path('C:\\Users\\my_files')

for i in range(number_of_days):
  new_date = datetime.datetime.now() - datetime.timedelta(days=i)
  year = new_date.strftime("%Y")
  month = new_date.strftime("%m")
  day = new_date.strftime("%d")

  file_path = os.path.join( year, month, day)
  print(file_path)

You can see how I format the date with strtime using the guide from here to get the appropriate strings.

Upvotes: 1

Related Questions