通过文件夹进行问题迭代并查找特定文件的时间戳
Problem iteration through folders and finding the timestamp for specific files
我写了一个函数 i Python,它遍历根文件夹,其中包含文件夹中的不同文件夹。
这些文件夹包含许多不同的文件,但我特别只在最近 24 小时内创建的文件夹中查找 PNG 文件。
我写了这段代码,但不幸的是我得到了一个错误..
for root, dirs, files in os.walk(rootFolder):
# if(datetime.fromtimestamp(os.path.getctime(root)).strftime("%Y") == "2021"):
for file in files:
if file.endswith(".png"):
time = datetime.fromtimestamp(os.path.getctime(root)).strftime("%Y")
print(datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d"))
显然当我尝试打电话时:
print(datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d"))
我收到“FileNotFoundError: [WinError 2] 找不到文件: '001-001-001.png'
我不明白这个“未找到”部分,当它 returns 错误消息中给我的名字时???
如果我改为在函数内写入 root,我会得到一个结果并返回时间戳,但这些不是我需要的。
print(datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d"))
我正在考虑这样做:
for root, dirs, files in os.walk(rootFolder):
# if(datetime.fromtimestamp(os.path.getctime(root)).strftime("%Y") == "2021"):
for file in files:
if file.endswith(".png") and datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d") == datetime.now().strftime("%Y:%m:%d"):
time = datetime.fromtimestamp(os.path.getctime(root)).strftime("%Y")
print(datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d"))
我会将 png 文件时间戳与当前时间进行比较,并检查它们是否早于 24 小时/一天。但不幸的是,由于代码无法将“文件”作为getmtime() 函数中的输入..
我希望有人能帮助我。
脚本是否使用了预期的目录?
您可以检查目录:
print("current directory: ", os.getcwd())
print("root: ", root)
print("current file: ", file)
如果您从 PNG 文件夹中运行您的初始脚本应该可以工作。
或者您使用文件的完整路径,例如:
timestamp = os.path.getmtime(os.path.join(root, file))
print(datetime.fromtimestamp(timestamp).strftime("%Y:%m:%d"))
我写了一个函数 i Python,它遍历根文件夹,其中包含文件夹中的不同文件夹。
这些文件夹包含许多不同的文件,但我特别只在最近 24 小时内创建的文件夹中查找 PNG 文件。
我写了这段代码,但不幸的是我得到了一个错误..
for root, dirs, files in os.walk(rootFolder):
# if(datetime.fromtimestamp(os.path.getctime(root)).strftime("%Y") == "2021"):
for file in files:
if file.endswith(".png"):
time = datetime.fromtimestamp(os.path.getctime(root)).strftime("%Y")
print(datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d"))
显然当我尝试打电话时:
print(datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d"))
我收到“FileNotFoundError: [WinError 2] 找不到文件: '001-001-001.png'
我不明白这个“未找到”部分,当它 returns 错误消息中给我的名字时???
如果我改为在函数内写入 root,我会得到一个结果并返回时间戳,但这些不是我需要的。
print(datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d"))
我正在考虑这样做:
for root, dirs, files in os.walk(rootFolder):
# if(datetime.fromtimestamp(os.path.getctime(root)).strftime("%Y") == "2021"):
for file in files:
if file.endswith(".png") and datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d") == datetime.now().strftime("%Y:%m:%d"):
time = datetime.fromtimestamp(os.path.getctime(root)).strftime("%Y")
print(datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y:%m:%d"))
我会将 png 文件时间戳与当前时间进行比较,并检查它们是否早于 24 小时/一天。但不幸的是,由于代码无法将“文件”作为getmtime() 函数中的输入..
我希望有人能帮助我。
脚本是否使用了预期的目录?
您可以检查目录:
print("current directory: ", os.getcwd())
print("root: ", root)
print("current file: ", file)
如果您从 PNG 文件夹中运行您的初始脚本应该可以工作。
或者您使用文件的完整路径,例如:
timestamp = os.path.getmtime(os.path.join(root, file))
print(datetime.fromtimestamp(timestamp).strftime("%Y:%m:%d"))