【问题标题】:touch a directory in python (Linux) [duplicate]触摸python(Linux)中的目录[重复]
【发布时间】:2016-09-21 22:46:58
【问题描述】:

有没有办法使用 python “触摸” Linux 中的现有目录,使其 modtime 成为当前系统时间?

在命令行中,这相当于touch $directory

【问题讨论】:

    标签: python linux python-3.x


    【解决方案1】:

    您可以使用os.utime

    now = time.time()
    os.utime('/tmp/marker', (now, now))
    

    【讨论】:

    • 现在不需要将 time.time() 转换为 int
    • 完全不需要使用time.time()——从Python 2.0开始,你只需使用os.utime('/tmp/marker', None)就可以达到同样的效果。
    【解决方案2】:

    os.utime() 将允许您设置现有文件系统对象的 atime 和 mtime,默认为当前日期和时间。

    os.utime(path)
    

    【讨论】:

    • 适用于 py3 但 py2,必须指定 atime mtime 元组
    • 其实,对于 py2,虽然您确实需要第二个参数,但可以简单地使用 None 来获取当前日期和时间。
    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    相关资源
    最近更新 更多