0

In Python, I have to write a code that selects from the directory a file starting with a certain string & there are multiple files with same name i need the latest as per date modified . Foe Example i have a file with name

StockPriceReport06112018.pdf    #First saved report in the mrng
StockPriceReport06112018(2).pdf #Updated report saved in same folder in the aftn
StockPriceReport06112018(3).pdf #Updated report saved in same folder in the evening

How to write a code to automate and open the latest file

Ayush
  • 41,754
  • 51
  • 164
  • 239
  • 1
    Refer [Link](https://stackoverflow.com/questions/39327032/how-to-get-the-latest-file-in-a-folder-using-python) – Lijo Jose Nov 06 '18 at 05:24

2 Answers2

0

If you want to open the file with the largest version number in the format you have provided, you can continue to try to open files with increasingly large version numbers until the file does not exist and you receive a FileNotFoundError.

try:
    version = ''
    version_number = None
    with open('file_name%s.pdf' % version) as f:
        pass

    version_number = 1
    while True:
        with open('file_name(%s).pdf' % version_number) as f:
            pass
        version_number += 1

except FileNotFoundError:
    if version_number is None:
        latest_file_name = 'file_name%s.pdf' % version
    else:
        latest_file_name = 'file_name(%s).pdf' % version

This assumes that your file version numbers are a continuous range, meaning that you are not missing a particular version of the file in your folder. So to find file(3).pdf, you need to have both file.pdf and file(2).pdf stored in the same folder.

Samantha
  • 275
  • 1
  • 12
0

I would open the file based on the modification time on the machine's file system. This involves doing a recursive file-list, then calling stat() on each file to retrieve the last-modified date:

EDIT: I mis-read the question, you actually want the newest file (I was finding the oldest)

import os
import sys
import glob

DIRECTORY='.'

### Builds a recursive file list, with optional wildcard match
### sorted so that the oldest file is first.
### Returns the name of the oldest file.
def getNewestFilename(path, wildcard='*'):
    age_list = []
    # Make a list of [ <modified-time>, <filename> ]
    for filename in [y for x in os.walk(path) for y in glob.glob(os.path.join(x[0], wildcard))]:
        modified_time = os.stat(filename).st_mtime
        age_list.append([modified_time, filename])

    # Sort the result, oldest-first
    age_list.sort(reverse=True)
    if (len(age_list) > 0):
        return age_list[0][1]
    else:
        return None


latest_file = getNewestFilename(DIRECTORY, 'StockPriceReport*.pdf')
if (latest_file != None):
    print("Newest file is [%s]" % (latest_file))
    data = open(latest_file, "rb").read()
    # ...
else :
    print("No Files")
Kingsley
  • 14,398
  • 5
  • 31
  • 53