【问题标题】:move all folders modified after <time> to new folder [closed]将 <time> 之后修改的所有文件夹移动到新文件夹 [关闭]
【发布时间】:2015-06-05 12:05:44
【问题描述】:

我需要编写一个脚本来移动给定父目录中的所有文件夹,这些文件夹在给定时间后被修改。 我想使用 bash 或 Python。

所以应该是这样的。

forall ${DIR} in ${PARENT_DIR}
If ${DIR} is modified after ${TIME}
move ${DIR} to ${NEW_DIR}

它必须每 15 分钟检查一次目录的修改并移动任何新创建的目录。

感谢您的帮助

【问题讨论】:

  • 我认为你应该使用 bash。检查“find”命令,尤其是“-mtime”和“-exec”键。你甚至不应该使用循环。
  • 你有什么问题?

标签: python bash last-modified


【解决方案1】:
import os
from shutil import move
from time import time

def mins_since_mod(fname):
    """Return time from last modification in minutes"""
    return (time() - os.path.getmtime(fname)) / 60

PARENT_DIR = '/some/directory'
MOVE_DIR = '/where/to/move'

# Loop over files in PARENT_DIR
for fname in os.listdir(PARENT_DIR):
    # If the file is a directory and was modified in last 15 minutes
    if os.path.isdir(fname) and mins_since_mod(fname) < 15:
        move(fname, MOVE_DIR) # move it to a new location

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2020-12-26
    • 2020-08-30
    相关资源
    最近更新 更多