0

我想编写一个脚本(phyton,powershell),它将提供一个用于统计目的的时间戳列表。这是关于视频文件 (*.mov)。对于每个拍摄日,我的文件夹结构中都有大量文件。因此,出于内部统计目的,我需要早上第一个拍摄文件和晚上最后一个拍摄文件的时间戳。

文件夹结构:CUSTOMER/MMDDYYYY/SUBFOLDER(S)/ManyVideoFiles.mov

结果应为每天和客户提供一份列表,其中包含最早和最新视频文件的时间戳。

For example:
CUST_AA/02122021   Earliest: 10:02:23
CUST_AA/02122021   Latest:   19:28:05
CUST_AA/02122022   Earliest: 09:18:45
CUST_AA/02122022   Latest:   17:15:37
CUST_BB/02122021   Earliest: 08:10:10
CUST_BB/02122021   Latest:   15:17:55 

有什么建议我该怎么做?应该没什么大不了的吧?:-))

谢谢和最好的

4

2 回答 2

0

你可以试试这个。如有疑问或问题,请询问。

#https://stackoverflow.com/questions/66171129/script-to-search-file-folder-srcipt

import os
import time

EXT = '.mov'

def get_first_last_cdate_in_folder(folder):
    '''return creation time of oldest and newest file.'''
    first = 0
    last = 0
    for file in os.scandir(folder):
        if file.name.endswith(EXT):
            ctime = os.stat(file).st_ctime
            if first == 0:
                first = ctime
            else:
                first = min(first, ctime)
            last = max(last, ctime)
    return first, last

# ADJUST to folder containing customer folders
CUST_FOLDERS_ROOT = r'C:\adjust\according\to\your\needs'

for root, dirs, files in os.walk(CUST_FOLDERS_ROOT):
    #print(root, dirs, files)
    if files:
        first, last = get_first_last_cdate_in_folder(root)
        #print(root)
        cust = root.split('\\')[-2]
        date = root.split('\\')[-1]
        print(cust, date, "Earliest:", time.strftime("%H:%M:%S",time.gmtime(first)))
        print(cust, date, "Latest  :", time.strftime("%H:%M:%S",time.gmtime(last)))
        #print()

于 2021-02-17T22:40:23.003 回答
0

这里:

import os,time

customer_id = input("customer id:")

def get_files(path):
    #print(path)
    out = []
    for i in os.listdir(path):
        if os.path.isfile(os.path.join(path,i)):
            #print(i,"is a file")
            out.append(os.path.join(path,i))
        else:
            #print(i,"is a folder")
            out += get_files(os.path.join(path,i))
    return out

days = os.listdir(customer_id)
for i in days:
    last       = 0.0
    first      = 99999999999999.0
    last_path  = None
    first_path = None
    for j in get_files(customer_id+"\\"+i):
        if j.endswith(".mov"):
            if os.path.getmtime(j) > last:
                last = os.path.getmtime(j)
                last_path = j
            if os.path.getmtime(j) < first:
                first = os.path.getmtime(j)
                first_path = j
    print(i,"first:",time.strftime("%H:%M:%S",time.gmtime(first)),"last:",time.strftime("%H:%M:%S",time.gmtime(last)))

抱歉没有解释代码,但我没有精力。

于 2021-02-12T12:22:41.197 回答