Home >Backend Development >Golang >Use the os.Chtimes function to modify the access and modification time of a file or directory

Use the os.Chtimes function to modify the access and modification time of a file or directory

WBOY
WBOYOriginal
2023-07-24 12:05:151546browse

Use the os.Chtimes function to modify the access and modification time of a file or directory

In Python, we can use the Chtimes function provided by the os module to modify the access and modification time of a file or directory. This is useful for applications that need to manage timestamps for files or directories.

The prototype of the Chtimes function is as follows:
os.Chtimes(path, times)

Among them, path is the path of the file or directory to modify the time, and times is a float containing two A tuple of points (access time and modification time).

Next, we use an example to demonstrate how to use the os.Chtimes function.

First, we create an empty text file named example.txt and obtain its access and modification times:

import os
import time

# 创建空文本文件
with open('example.txt', 'w'):
    pass

# 获取文件的访问和修改时间
atime = os.path.getatime('example.txt')
mtime = os.path.getmtime('example.txt')

print('原始访问时间:', time.ctime(atime))
print('原始修改时间:', time.ctime(mtime))

Next, we use the Chtimes function to modify the access and modification of the file Time:

from datetime import datetime
import os

# 修改文件的访问和修改时间
new_mtime = datetime.now().timestamp()  # 获取当前时间并转换为时间戳
new_atime = os.path.getatime('example.txt')  # 保持原始访问时间不变
os.chtimes('example.txt', (new_atime, new_mtime))

# 获取修改后的访问和修改时间
atime = os.path.getatime('example.txt')
mtime = os.path.getmtime('example.txt')

print('修改后的访问时间:', datetime.fromtimestamp(atime))
print('修改后的修改时间:', datetime.fromtimestamp(mtime))

Run the above code, you will see the output is similar to the following:

原始访问时间: Wed Jan 26 15:32:05 2022
原始修改时间: Wed Jan 26 15:32:05 2022
修改后的访问时间: Wed Jan 26 15:32:05 2022
修改后的修改时间: Wed Jan 26 15:39:15 2022

Through the above code example, we successfully used the os.Chtimes function to modify example.txt The modification time of the file is the current time, and the access time remains unchanged.

It should be noted that if you try to modify the timestamp of a directory, you need to ensure that your Python script has sufficient permissions to perform the operation.

Summary:
Using the os.Chtimes function can easily modify the access and modification time of a file or directory. We can customize the timestamp by passing a tuple containing access and modification times. This function is useful in applications that need to manage file or directory timestamps.

The above is the detailed content of Use the os.Chtimes function to modify the access and modification time of a file or directory. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn