Önce bir fonksiyon get_latest_file
def get_latest_file(path, *paths):
fullpath = os.path.join(path, paths)
...
get_latest_file('example', 'files','randomtext011.*.txt')
Ayrıca docstring'ini kullanabilir define (cevabını geliştirmek için Düzenlendi)! Python 3 kullanırsanız
def get_latest_file(path, *paths):
"""Returns the name of the latest (most recent) file
of the joined path(s)"""
fullpath = os.path.join(path, *paths)
, bunun yerine iglob kullanabilirsiniz.
Komple kod son dosyanın adını döndürmek için:
def get_latest_file(path, *paths):
"""Returns the name of the latest (most recent) file
of the joined path(s)"""
fullpath = os.path.join(path, *paths)
files = glob.glob(fullpath) # You may use iglob in Python3
if not files: # I prefer using the negation
return None # because it behaves like a shortcut
latest_file = max(files, key=os.path.getctime)
_, filename = os.path.split(latest_file)
return filename
dosya bulmaya gibidir: Böyle (denenmemiş) olarak
? İlgili kodu quesiton'a ekleyin. –
Neden sizin için çalışmıyor olabileceğini tahmin ediyorum: dosya adı dosyalarının bir listesi veya tek bir dosya dizisi "dosyaları" mı? – mpurg