I am a beginner in python and trying to modify access times (touch
) of sub-directories and files of a parent directory. I found that how to modify access time of a file here Implement touch using Python?
import os def touch(fname, times=None): with open(fname, 'a'): os.utime(fname, times)
instead of fname
above, I want to have sub-directories and files of a parent directory. So I found another question of looping through directories: Iterating through directories with Python
import os rootdir = 'C:/Users/sid/Desktop/test' for subdir, dirs, files in os.walk(rootdir): for file in files: print os.path.join(subdir, file)
Combining code from above examples, I created code:
import os
rootdir = '/usr/sf/adir'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
fname = os.path.join(subdir, file)
def touch(fname, times=None):
with open(fname, 'a'):
os.utime(fname, times)
The code runs without errors, but when I did ls -l
, I could not see access time stamp modified. Where am I going wrong? Is the third code correct to touch
all files and sub-directories?
I am using python 2.6.