简体   繁体   English

Python中的最后修改文件?

[英]Last Modified File in Python?

So I'm trying to write a script that allows me import the most recently modified file from a directory. 因此,我正在尝试编写一个脚本,该脚本允许我从目录中导入最新修改的文​​件。 I've looked at the glob and os.listdir commands but they don't seem to do it (I get errors). 我已经看过globos.listdir命令,但它们似乎没有做到(我得到了错误)。 Any thoughts? 有什么想法吗?

import os
import glob
newest = max(glob.iglob('Directory Name'), key=os.path.getctime)
print newest
f = open(newest,'r')

I get an error: 我收到一个错误:

max() arg is an empty sequence max()arg是一个空序列

Would something like os.stat work better? 像os.stat这样的东西会更好吗?

尝试:

newest = max(glob.iglob('Directory Name/*'), key=os.path.getctime)

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

相关问题 如何获取上次在Python中修改文件的时间? - How do I get the time a file was last modified in Python? 使用Python生成文件创建日期和上次修改日期 - Generating file created date and last modified date with Python 如何使用python或django查找最后修改的png文件 - How to find the last modified png file using python or django 如何使用Python获取文件夹中上次修改文件的时间 - How to get the time of the last modified file in a folder using Python 如何使用 Python 列出 S3 中最后修改的文件 - How to list last modified file in S3 using Python Python zipFile模块unzipfile如何保持文件的上次修改时间? - How Python zipFile module unzipfile keep file last modified time? python / pandas - 从目录导入 last modified.xlsx 文件 - python / pandas - Import last modified .xlsx file from a directory Python如何从TeamSites读取文件的最后修改日期 - Python how to Read last modified date of file from TeamSites GAE Python:如何获取静态文件的最后修改日期 - GAE Python: how get last modified date of static file 谷歌云存储(Python):如何检查文件上次修改时间? - Google Cloud Storage(Python): How to check when was file last modified?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM