2

I have two directory with .xlsb and .msg extension i.e. for monthly and weekly. I want to get the most recent file from each folder.

I am using the same code for both, but I am getting error only for weekly folder.

self.weekly_file = [os.path.join(self.weekly_path, x) for x in os.listdir(self.weekly_path) if
                          x.endswith(".xlsb")]
    
print(self.weekly_file)
self.newest_weekly_file = os.path.basename(max(self.weekly_file, key=os.path.getctime))
print(self.newest_weekly_file)

I am getting error as:

return os.stat(filename).st_ctime
OSError: [WinError 1] Incorrect function: '\\\\docs.xyz.net.au\\sites\\K7777\\Reports\\Week\\Week - 2021-05-10.xlsb'

Even I tried the below code, this is giving only the folder name.

self.weekly_file = glob.glob(self.weekly_path)
6
  • 1
    can you try to sort the files by modification date? Commented Aug 3, 2021 at 9:40
  • @rock Can you please help me how can I do that, I am new to Python. Commented Aug 3, 2021 at 10:09
  • Looks like your files are not local which might be a problem. Your files seem to have timestamps. If you can rely on them then just use max without a key-function? Commented Aug 3, 2021 at 10:31
  • which line give raise to the error? have you isolated it? Your strategy seems good, guess joining the paths could be the problem Commented Aug 3, 2021 at 15:03
  • @Timus Thanks , It solved my problem, just wanted to know if it can be reliable because every week 1 file will be added and I have to pick the latest one and process it. Commented Aug 3, 2021 at 15:34

1 Answer 1

1

Try this instead:

xlsb_weekly = glob.glob('self.weekly_path/*.xlsb')
newest_weekly_file = max(xlsb_weekly, key=os.path.getctime)
print(newest_weekly_file)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.