【发布时间】:2026-01-22 01:10:01
【问题描述】:
我对 bash 比较陌生。我想知道 Mac OS 终端上是否有一个现有命令可以在新目录中按月分隔视频文件。从本质上讲,这意味着如果我在 12 个月内有视频,我将有 12 个新目录,其中包含视频。
否则,我想知道是否有另一种方法可以通过 python 来解决此类问题。
我希望用它来处理 500 多个视频文件。如果我可以为我编写一个脚本而不是一个一个地浏览它们,那将极大地帮助我。
脚本之前
脚本后(期望的输出)
更新(找到解决方案)
我最终找到了解决方案,感谢您引导我找到正确的答案。现在我今天学到了一件新东西
import os, shutil
from datetime import datetime
filepath = "/Users/alfietorres/Downloads/" #base file path
for filename in os.listdir(filepath): #iterate through each file in the directory
r = os.stat(filepath+filename) #look for the file path and the file name
d = r.st_birthtime #look for the time created
date=datetime.fromtimestamp(d) #assign the details to a date variable
month_directory = filepath+str(date.month)+"-"+str(date.year) #use the date variable to create a UNIQUE new directory
file_being_moved = filepath+filename #file the path of the file being moved
if os.path.isdir(month_directory) : #check if the directory is created
print("directory found ... ")
shutil.move(file_being_moved,month_directory) #move the file we are iterating on to the directory
else:
print("creating directory ... ")
os.mkdir(month_directory) #create new directory
shutil.move(file_being_moved,month_directory) #move items in new directory
【问题讨论】:
-
是的,这很简单。你怎么知道哪个视频属于哪个月份?它在文件名中吗?元数据?修改时间?
-
对不起,我又检查了一遍。我再次修复了OP。我发现有一种方法可以按 创建日期 分隔文件。有没有办法自动做到这一点?
-
创建日期和你从
GetFileInfo -d yourfile.mov得到的日期一样吗? -
是的,它是输入:GetFileInfo -d IMG_2997\ 2.HEIC 输出:02/15/2020 17:36:59。与获取文件信息 GUI 相同
-
所以您对
.MOV或.mov感兴趣?
标签: python bash macos terminal file-management