“open(file_name,'a').close()”在Windows上的python 2.7中不适用于我。”utime(文件名,无)工作得很好。
另外,我需要递归地触摸目录中日期早于某个日期的所有文件。我根据埃弗米恩特非常有用的回答创建了一个追随者。
def touch(file_name):
# Update the modified timestamp of a file to now.
if not os.path.exists(file_name):
return
try:
os.utime(file_name, None)
except Exception:
open(file_name, 'a').close()
def midas_touch(root_path, older_than=dt.now(), pattern='**', recursive=False):
'''
midas_touch updates the modified timestamp of a file or files in a
directory (folder)
Arguements:
root_path (str): file name or folder name of file-like object to touch
older_than (datetime): only touch files with datetime older than this
datetime
pattern (str): filter files with this pattern (ignored if root_path is
a single file)