Reputation: 161
Im trying to get the timestamps of all of the files within a text file.
Files listed in text file:
folder/file1
folder/file2
System should output timestamp of:
folder/file1 timestamp-of-file
folder/file2 timestamp-of-file
Here is my code:
import os
f = open('config.dat','r')
list_contents = f.read().split('\n')
timestamp=os.path.getmtime(list_contents)
for a in timestamp:
print(timestamp)
f.close()
Upvotes: 2
Views: 690
Reputation: 189347
Reading the entire file into memory is unnecessary and potentially wasteful.
with open(config.dat) as inputfile:
for line in inputfile:
filename = line.rstrip('\n')
print(filename, os.path.getmtime(filename))
The getmtime()
return value is a Unix timestamp; you might want to pass it to time.strftime('%c', time.localtime(os.path.getmtime(filename))
for human-readable output.
Upvotes: 0
Reputation: 2005
Try this:
import os
f = open('config.dat','r')
list_contents = f.read().split('\n')
f.close()
for a in list_contents:
print(a, os.path.getmtime(a))
os.path.getmtime(path)
returns the timestamp of the file to be found under path
, so you have to pass each of the entries of list_contents
separately to this function.
Upvotes: 1