简体   繁体   中英

How to get the latest folder in a directory using Python

I need to retrieve the directory of the most recently create folder. I am using a program that will output a new run## folder each time it is executed (ie run01, run02, run03 and so on). Within any one run## folder resides a data file that I want analyze (file-i-want.txt).

folder_numb = 'run01'
dir = os.path.dirname(__file__)
filepath = os.path.join(dir, '..\data\directory',run_numb,'file-i-want.txt')

In short I want to skip having to hardcode in run## and just get the directory of a file within the most recently created run## folder.

You can get the creation date with os.stat

path = '/a/b/c'

#newest

newest = max([f for f in os.listdir(path)], key=lambda x: os.stat(os.path.join(path,x)).st_birthtime)

# all files sorted

sorted_files = sorted([f for f in os.listdir(path)],key=lambda x: os.stat(os.path.join(path, x)).st_birthtime, reverse=True)

glob.glob('run*') will return the list of files/directories that match the pattern ordered by name.

so if you want the latest run your code will be:

import glob
print(glob.glob('run*')[-1])  # raises index error if there are no runs

IMPORTANT, the files are ordered by name, in this case, for example, 'run21' will come AFTER 'run100', so you will need to use a high enough number of digits to not see this error. or just count the number of matched files and recreate the name of the folder with this number.

you can use glob to check the number of files with the same name pattern:

import glob
n = len(glob.glob('run*')) # number of files which name starts with 'run'
new_run_name = 'run' + str(n)

Note: with this code the file names starts from 0, if you want to start from 1 just add 1 to n.

if you want always double digit run number (00, 01, 02) instead of 'str(n)' use 'str(n).zfill(2)'

example:

import glob
n = len(glob.glob('run*')) # number of files which name starts with 'run'
new_run_name = 'run' + str(n + 1).zfill(2)

pathlib is the recommeded over os for filesystem related tasks.

reference

You can try:

filepath = Path(__file__).parent / 'data/directory'
fnames = sorted(list(Path(filepath).rglob('file-i-want.txt')), key=lambda x: Path.stat(x).st_mtime, reverse=True)
filepath = str(fnames[0])
filepath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM