python - glob.iglob Remove path from filename -


i trying recent file added directory using python 2.7 os , glob modules.

import os import glob  path = "files/" newestfile = max(glob.iglob(path + '*.txt'), key=os.path.getctime)  print newestfile 

when print newestfile variable path included i.e.

files\file.txt

i want filename .txt file , .py script not in same directory. text file 1 directory down under files directory. how refer directory , newest .txt file added directory.

you can use os.path.basename filename:

newestfile = os.path.basename(max(glob.iglob(path + '*.txt'), key=os.path.getctime)) 

os.path.getctime going need full path 1 way or have use full path.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -