折腾:
【未解决】对接抓包平台新增更新任务实时进度
期间,需要去获取一个文本文件的最后更新时间,最好返回的是时间戳,13位的。
python file update time
os.path.getmtime(path)
Return the time of last modification of path. The return value is a floating point number giving the number of seconds since the epoch (see the time module). Raise OSError if the file does not exist or is inaccessible.
Changed in version 3.6: Accepts a path-like object.
去试试
同时进来支持异常情况,包括文件不存在之类的
import os def getUpdateTime(curFileOrPath): """get file/folder latest update time = modify time Args: curFileOrPath (str): some file or folder path Returns: time stamp (int) of 13 digit, with milliseconds Raises: """ updateTime = None try: modifyTime = os.path.getmtime(curFileOrPath) updateTime = int(modifyTime * 1000) except OSError as err: pass return updateTime
然后去测试看看

是可以的。
再去测试其他:
folder目录:也是可以的:

以及不存在的路径
也是不会跑出异常的,只是print打印而已
"[Errno 2] No such file or directory: '/xx/xxx'"

是对的。
【总结】
最后用代码:
import os def getUpdateTime(curFileOrPath): """get file/folder latest update time = modify time Args: curFileOrPath (str): some file or folder path Returns: int: time stamp int of 13 digit, with milliseconds Raises: """ updateTime = None try: modifyTime = os.path.getmtime(curFileOrPath) # 1593748641.3270357 updateTime = int(modifyTime * 1000) # 1593748641327 except OSError as err: errMsg = str(err) # print("errMsg=%s" % errMsg) pass return updateTime
即可支持:
输入文件或目录的路径,输出文件最后修改时间=最后更新时间,格式是:13位的int的时间戳
转载请注明:在路上 » 【已解决】Python中获取文件最后更新时间