0

I have an assignment where I must write a script that runs before my main program and basically it checks if the main script has been modified by checking 2 other exact copies of the scripts that act as control variables to see if it's modified. The break is essentially me exiting this script and then going back to the first script ran which runs this check script and then the main script in sequential order assuming everything goes right. I am running Python 3.7. What keeps happening is that it runs the else clause I put in the bottom. main2 and main3 are exact copies of main. I also tried replacing == with is to not avail.

    program = 25
    while program == 25:
      checkmain = open('main.py','r')
      main2 = open('main2.py','r')
      main3 = open('main3.py','r')
      if main2 and main3 == checkmain:
        checkmain.close()
        main2.close()
        main3.close()
        break
      else:
        print("ERROR")
        exit()
  • 1
    no this will never work. Not only are you using and incorrectly but you cant compare a files contents by comparing the file objects for equality. You'll need to read the contents of the file and compare them that way or use some sort of hash function on the file contents. There is the [`filecmp`](https://docs.python.org/3.7/library/filecmp.html) module available to do this. – Paul Rooney Jun 15 '19 at 05:33

1 Answers1

0

You can compare the content of 2 files as follows:

import filecmp
if filecmp.cmp('main.py', 'main2.py') and filecmp.cmp('main2.py', 'main3.py'):
    break
else:
    print("ERROR")
    exit()

refer to this answer for further help.

Shadowfax
  • 556
  • 2
  • 13