Reputation: 289
I am creating a python script that will access each line from a Text file(say File.txt) one by one then search for corresponding '.py' and '.txt' file in the system directory. For example if "COPY"(the first line) is accessed from "File.txt" then search will be done for "COPY.py" and "COPY.txt". If both the files are found then their modification date will be compared. Code have no syntax error But I am getting the wrong output.
My Python code is:
for line in fileinput.input(r'D:\Python_Programs\File.txt'):
line = line[0:-1]
sc = ''.join((line,'.py'))
lo = ''.join((line,'.txt'))
for root, dirs, files in os.walk(r'D:\txt and py'):
if sc in files:
pytime = time.ctime(os.path.getmtime(os.path.join(root, sc)))
print(sc, ' :', pytime)
for root, dirs, files in os.walk(root):
if txt in files:
txttime = time.ctime(os.path.getmtime(os.path.join(root, txt)))
print(txt, ' :', txttime)
if (txttime > pytime):
print('PASS', '\n')
else:
print('FAIL', '\n')
Output:
COPY.py : Mon Aug 27 10:50:06 2012
COPY.txt : Mon Feb 04 11:05:31 2013
PASS #Expected = PASS
COPY2.py : Fri Feb 08 16:34:43 2013
COPY2.txt : Sat Sep 22 14:19:32 2012
PASS #Expected = FAIL
COPY3.py : Fri Feb 08 16:34:53 2013
COPY3.txt : Mon Sep 24 00:50:07 2012
PASS #Expected = FAIL
I am not getting why "COPY2" and "COPY3" are giving "PASS". May be I am doing it in a wrong way. As well as on changing the comparison as "if (txttime < pytime)" in the code. All results are showing as "FAIL" in output.
Upvotes: 20
Views: 28945
Reputation: 5127
I liked copying the bash file test -nt (newer than) and -ot (older than) logic for use-cases where you might want to test whether to process old files into new files:
def test_file_newer_than(file1,file2):
"""
True if file1 is newer (according to modification date) than
file2, or if file1 exists and file2 does not.
(like bash file test operator -nt)
"""
import os.path
import datetime
retval = os.path.exists(file1)
if retval and os.path.exists(file2):
mtime1 = os.path.getmtime(file1)
mtime2 = os.path.getmtime(file2)
newer = mtime1 - mtime2
retval = newer > 0
#print(F"{mtime1}-{mtime2} = {newer}")
return retval
def test_file_older_than(file1,file2):
"""
True if file1 is older (according to modification date) than
file2, or if file2 exists and file1 does not.
(like bash file test operator -nt)
"""
import os.path
import datetime
retval = os.path.exists(file2)
if retval and os.path.exists(file1):
mtime1 = os.path.getmtime(file1)
mtime2 = os.path.getmtime(file2)
older = mtime2 - mtime1
retval = older > 0
#print(F"{mtime2}-{mtime1} = {older}")
return retval
Upvotes: 0
Reputation: 39950
time.ctime()
formats a time as a string, so you're comparing the strings "Fri Feb 08 16:34:43 2013"
and "Sat Sep 22 14:19:32 2012"
textually. Just don't do that and compare the float
s that getmtime()
gives you directly:
pytime = os.path.getmtime(os.path.join(root, sc))
# ...
txttime = os.path.getmtime(os.path.join(root, txt))
# ...
if (txttime > pytime):
# ...
Upvotes: 47
Reputation: 25693
time.ctime
returns a string and 'Fri Feb 08 16:34:53 2013' < 'Mon Sep 24 00:50:07 2012'
Upvotes: -1