简体   繁体   English

使用 Python,递归地为过去 24 小时内创建的 all.jpg 文件创建符号链接

[英]Using Python, recursively create symbolic links for all .jpg files created within the last 24 hours

I store my photo library organized by year and event, for example:我存储按年份和事件组织的照片库,例如:

/mnt/mediapool/images1/2020/Day at the beach/IMG1.JPG
/mnt/mediapool/images1/2020/Day at the beach/IMG2.JPG
/mnt/mediapool/images1/2021/Sunset/IMG15.JPG

Using Python3, how can I recurse through all my images and create symbolic links inside another directory that list all images created within the last 24 hours (and 7-days, 30-days, etc..)?使用 Python3,我如何递归遍历我的所有图像并在另一个目录中创建符号链接,该目录列出了过去 24 小时(以及 7 天、30 天等)内创建的所有图像?

For example:例如:

/mnt/mediapool/sorted/last-24h/IMG1.JPG (symbolic link to /mnt/mediapool/images1/2020...)
/mnt/mediapool/sorted/last-24h/IMG2.JPG (symbolic link to /mnt/mediapool/images1/2020...)
/mnt/mediapool/sorted/last-24h/IMG3.JPG (symbolic link to /mnt/mediapool/images1/2020...)
/mnt/mediapool/sorted/last-7d/IMG1.JPG (symbolic link to /mnt/mediapool/images1/2020...)
  1. Use os.walk(/mnt/mediapool/images1/) to recurse through that directory tree.使用os.walk(/mnt/mediapool/images1/)递归遍历该目录树。

    • remember to use os.path.join() to create a full path from the parent dir to the file记得使用os.path.join()创建从父目录到文件的完整路径
  2. For each file, check the creation time or the modified time with os.stat()对于每个文件,使用os.stat()检查创建时间或修改时间

    • compare with the current time in seconds and calculate 24hrs/7days/30days与当前时间(以秒为单位)比较并计算 24hrs/7days/30days
    • since you've got last 7 days including everything in the last 24 hours, use a loop to check each time-window and execute the next steps conditionally由于您已经过去 7 天,包括过去 24 小时内的所有内容,请使用循环检查每个时间窗口并有条件地执行后续步骤
  3. Create the necessary directory tree for where the link would go, using os.makedirs使用os.makedirs为链接 go 的位置创建必要的目录树

    • doesn't throw errors for existing dirs and allows creating a tree structure不会为现有目录抛出错误并允许创建树结构
  4. Create the link to the target image with os.symlink() or pathlib.Path.symlink_to()使用os.symlink()pathlib.Path.symlink_to()创建到目标图像的链接

    • (you may be able to skip step 3 when doing step 4) (您可以在执行第 4 步时跳过第 3 步)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 os 目录中获取过去 24 小时内创建的所有文件 - How to get all the files created in last 24 hours in os directory Python:列出最近24小时内创建的具有特定扩展名的子目录中的文件 - Python: List files in subdirectories with specific extension created in last 24 hours 增强Python脚本以下载在过去24小时内创建的Amazon S3文件 - Enhance Python script to download Amazon S3 files created in last 24 hours Python中递归遍历多个符号链接 - Traversing multiple symbolic links recursively in Python 获取过去 24 小时内创建的记录 - Get records created in last 24 hours 如何创建 python 脚本,以便当目录中的 csv 文件在过去 24 小时内未更新时发送 email? - How do I create a python script such that it sends an email when csv files in a directory has not updated in the last 24 hours? 无论如何要在过去 24 小时内修改文件而不遍历目录中的所有文件 - Is there anyway to get files modified in last 24 hours without looping through all files in directory Python 检查日期是否在 24 小时内 - Python check if date is within 24 hours Praw:如何获取过去 24 小时内创建的热门帖子? - Praw: How to get top posts that were created in last 24 hours? 递归查找所有链接使用bs4和Python问题 - Find all links recursively using bs4 and Python problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM