pythonの特定のディレクトリから最新のファイルの名前を取得しますか?linuxで最新のファイル名を取得する - PYTHON
私はこの
import os
import glob
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)
print fullpath
list_of_files = glob.glob(fullpath)
if not list_of_files:
return None
latest_file = max(list_of_files, key=os.path.getctime)
_, filename = os.path.split(latest_file)
return filename
if __name__ == "__main__":
print get_latest_file('ocr', 'uploads', '*.png')
を使用しかし、私はコードは、ファイルの拡張子を指定せずに最新のファイルの名前を返すようにしたいです。 jpg、jpeg、png、gifがあるとします。
スニペットですべてをカバーします。
入力はありますか?
glob.globで、ファイルの末尾の接尾辞を置き換えることもできます。あなたは試してみましたか?print get_latest_file( 'ocr'、 'uploads'、 '*') – hasan
@hasanそれは答えです。それをもっとはっきりと書きたいのですか? – bbastu