gpt4 book ai didi

python - 获取文件最后一次在python中编辑的时间

转载 作者:太空宇宙 更新时间:2023-11-03 19:06:33 24 4
gpt4 key购买 nike

我浏览过SO和其他各种网站,并认为我已经弄清楚了这一点,但显然我没有,或者我做错了什么。这是我尝试过的:

pre = datetime.fromtimestamp(f)
pre = os.path.getmtime(f)
pre = fromtimestamp(f)

所有三个都返回错误:

TypeError: an integer is required

我做了一些挖掘,发现了很多人的建议:

os.path.getmtime(path)¶ 
Return the time of last modification of path. The return value is a number giving the number of seconds since the epoch (see the time module). Raise os.error if the file does not exist or is inaccessible.

New in version 1.5.2.

Changed in version 2.3: If os.stat_float_times() returns True, the result is a floating point number.

所以现在我面临的问题是,如何让它成为一个整数值,以便我可以在读取文件后将这次与另一个值进行比较,以确定文件在解析时是否发生更改。

最佳答案

os.path.getmtime 采用文件路径,而不是文件对象:

>>> os.path.getmtime('/')
1359405072.0

如果f是一个打开的文件,请尝试传入f.name

关于python - 获取文件最后一次在python中编辑的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14572927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com