Python touch file

To touch a file in python, do the following:

import os
 
def touch(fname, times=None):
    """
    touch - change file timestamps
    """
    with file(fname, 'wa'):
        os.utime(fname, times)
 
if __name__ == '__main__':
    for i in range(0, 10):
        filename = 'somefile{i}.txt'.format(i=i)
        print(filename)
        touch(filename)
Did this help you? Please let me know with a comment. Thank you
Posted .

Comments (RSS)

Leave a comment