簡體   English   中英

Python:如何打開最后修改的文件夾

[英]Python: how to open last modified folder

我正在尋找使用python 2.7打開最新文件夾的代碼。

之后,我得到x張照片:

PATH = "/path/to/your/folder"
DST = "/destination/folder"

pictures = [os.path.join(PATH, pic) for pic in os.listdir(PATH)]
pictures.sort(key=os.path.getmtime)
return (pictures[-7:]) 

我可以復制的內容:

def kopieren():         
    for pic in get_last_pics():
        print(datetime.fromtimestamp(os.path.getmtime(pic)))
        shutil.copy2(os.path.join(pic), os.path.join(DST)) 

kopieren()

誰能給我個主意,我如何跳入最新文件夾以從中獲取圖片。

以下程序允許您獲取最后修改的文件夾的路徑:

import os, sys

def get_newest_folder(path):
    newest = None
    date = None

    for f in get_all_folders(path):
        if (date == None or date < os.path.getmtime(f)):
            newest = f
            date = os.path.getmtime(f)

    return os.path.join(path, newest)

def get_all_folders(path):
    return [x for x in os.listdir(path) if os.path.isfile(x) == False]

if __name__ == "__main__":
    print(get_newest_folder(sys.argv[1]))

這是一個解決方案,如何找到最新的文件夾:

在Python目錄中查找最新文件夾

... 一種

有用 !!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM