-1

I want to use Python to automate the search for a file modified on a certain day and time in a shared directory.

The manual step currently looks as follows:

*check file created with Previous business date in \\fcganas3\FICC\FIC_CMT\SecurityAnalytics\
or \\fcganas3\FICC\FIC_CMT\SecurityAnalytics\Load\ folder*
Filename: " BBG-BIGE-SPFD_1_YYYYMMDD.csv "  (YYYYMMDD - Previous Working Day).

I am able to search files in the local directory (refer below code)

    import os
    import glob
    
    path = 'e:\\'
    extension = 'pdf'
    os.chdir(path)
    result = glob.glob('*.{}'.format(extension))
    print(result)
goric
  • 11,491
  • 7
  • 53
  • 69

2 Answers2

0

When you go to the folder and use gl0b.glob you will get the list of files in the folder. Loop through the file names and extract the last 8 characters and match it with the date you want.

minnieme
  • 21
  • 5
0

You're looking for datetime and timedelta.

I have extended your code below with examples to answer your question.

import os
import glob
import os.path, time
from datetime import datetime, timedelta

path = 'e:\\'
extension = 'csv' # You asked for csv but your example had pdf...
os.chdir(path)
result = glob.glob('*.{}'.format(extension))

for fil in result:
    last_mod = datetime.fromtimestamp(os.path.getmtime(fil))
    print("last modified: %s" % last_mod)
    specific_day_and_time = datetime.strptime('2020-08-07 09:00:00', '%Y-%m-%d %H:%M:%S')
    print("Since specific time: {}".format(specific_day_and_time - last_mod))
    yesterday = datetime.now() - timedelta(days=1)
    if yesterday - last_mod  > timedelta(days=1):
        print("It's been more than a day since this file was updated")

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Lars Skaug
  • 1,376
  • 1
  • 7
  • 13
  • I am trying to run the above code but facing problem with the given location which is a shared path. – nisha verma Aug 14 '20 at 19:20
  • Your question says nothing about being unable to reach a shared folder. Please update your question accordingly. This might also help: https://stackoverflow.com/questions/2953834/windows-path-in-python. – Lars Skaug Aug 15 '20 at 14:10