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

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

c# - Search and Add Comment with OpenXML for Word -