如何使用Python递归地touch所有文件?

如何使用Python递归地touch所有文件?

要递归地touch所有文件,您需要使用os.walk遍历目录树并使用os.utime(path_to_file)将其中的所有文件all touch。

更多Python相关文章,请阅读:Python 教程

示例

import os
# 递归地遍历所有文件
for root, dirs, files in os.walk(path):
    for file in files:
        # 将utime设置为当前时间
        os.utime(os.path.join(root, file))

Python 3.4+中,可以直接使用pathlib模块来touch文件。

示例

from pathlib import Path
import os
# 递归地遍历所有文件
for root, dirs, files in os.walk(path):
    for file in files:
        Path(os.path.join(root, file)).touch()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程