Manipulating Files and Calling System Commands in Python
When I practiced NOIP exercises, I used Windows PC with a
.bat file to execute my program and compare my answer files with the correct answer files. However, for those days, I mainly use my MacBook to work and
.bat is not executable on macOS. So I have to write my own script to execute my program and compare my answer files with the correct answer files.
- Copy test sample input file content into my program's input file, like
copycommand in Windows Command Line
- Execute my program, like
callcommand in Windows Command Line
- Compare my program's output answer file with test sample answer file, like
fccommand in Windows Command Line
My implementation is as following.
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import datetime import shutil import filecmp if __name__ == '__main__': for i in range(1, 6): ## copy frog%1.in frog.in >nul shutil.copyfile('frog%s.in' % i, 'frog.in') print('Problem Test Data %s' % i) ## also `''.format()` or `x , y` print(str(datetime.datetime.now())) ## TODO: frog.exe os.system('./frog.bin') ## also subprocess.call(['./frong.bin']) print(str(datetime.datetime.now())) # same = filecmp.cmp('frog.out', 'frog%s.out' % i) same = (open('frog%s.out' % i, 'r').read().strip() == open('frog.out', 'r').read().strip()) if same: print('Okay') else: print(open('frog%s.out' % i, 'r').read()) print(open('frog.out', 'r').read()) ## pause input()
* cached version, generated at 2019-06-22 12:12:32 UTC.
Subscribe by RSS