-2
import os  
from datetime import datetime  

InFileName = r'path\Snapshot_list.txt'
#OutFileName = r'path\Process.bat'

InFile = open(InFileName)
for line in InFile:

s='20'
line = s[:2] + line[2:] [:6]
datetimeobject = datetime.strptime(line,'%Y%m%d')
newformat = datetimeobject.strftime('%Y-%m-%d')

print (newformat)

InFile.close()

The format I get is :

2012-05-30
2012-05-31
2012-06-01
2012-06-02
2012-06-03
2012-06-05

I want to compare every line with the line before , if line 2 != line 1 + 1 then I will write 'things' in outfile

  • Save the previous value for the next iteration? – ivan_pozdeev Jan 20 '15 at 11:21
  • Yes it consists in saving the previous value for the next iteration, but do I need to set a condiftion IF in the same loop or save the list in a temporary file where I do a new iteration? I am a beginner could you please help? – user2106896 Jan 20 '15 at 11:26
  • See http://stackoverflow.com/questions/14813624/compare-lists-in-python-while-looping/14813874#14813874 for an example – ivan_pozdeev Jan 20 '15 at 11:30

1 Answers1

1
from datetime import timedelta    

prev_date= None
for line in InFile:
    datetimeobject = datetime.strptime(line,'%Y%m%d')
    if datetimeobject-timedelta(days=1) != prev_date:
        outfile.write('things')
    prev_date= datetimeobject
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • for clarity, you could use `expected_date = datetime_object + DAY`. Note: it doesn't matter in this case but in general [*do not subtract naive datetime objects that represent local time*; it fails in many cases](http://stackoverflow.com/a/26313848/4279). Also, [24 hours ago and yesterday could be different time moments](http://stackoverflow.com/q/15344710/4279) (again, it does not matter in this particular case because the input does not specify the time). See also, [How can I subtract a day from a python date?](http://stackoverflow.com/q/441147/4279) – jfs Jan 21 '15 at 00:06