This repository has been archived by the owner on Mar 23, 2020. It is now read-only.
/
ReadOutput.py
62 lines (51 loc) · 1.75 KB
/
ReadOutput.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import subprocess
import os
import shutil
testdir = './Tests/'
tempdir = './temp/'
def readOutput(f):
# Make temporary directory
if not os.path.exists(tempdir):
os.makedirs(tempdir)
# remove system pause
# tf = open(f, 'r')
# lines = tf.readlines()
# tf.close()
# tf = open(f, 'w')
# for line in lines:
# if 'system("PAUSE");' not in line:
# tf.write(line)
# tf.close()
subprocess.run('gcc ' + f + ' -o ./temp/out.exe')
# Method 1: (will have to remove all the 'system pause')
# result = subprocess.run('a.exe', stdout=subprocess.PIPE, input='\n'.encode('utf-8'))
# result = subprocess.run('a.exe', input='\n'.encode('utf-8'))
# result = subprocess.run('temp\\out.exe', input='\n'.encode('utf-8'), shell=True)
# print(result)
# Method 2:
# process = subprocess.Popen(['a.exe'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
# process.communicate(input='\n')
# Method 2 Attemp 2:
# process = subprocess.Popen(
# ['temp\\out.exe'],
# stdin=subprocess.PIPE,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE
# )
# inputdata = "Muchen\n".encode('utf-8')
# stdoutdata, stderrdata = process.communicate(input=inputdata)
# stdoutdata = str(stdoutdata)
# stdoutdata = stdoutdata.replace('b\'', '')
# stdoutdata = stdoutdata.replace('\\r\\n', '\r\n')
# print(stdoutdata)
# Method 3
# os.system('temp\\out.exe')
# Method 4
rc = subprocess.call('start temp\\out.exe', shell=True)
# Delete the temorary folder
shutil.rmtree(tempdir)
for file in os.listdir(testdir):
fname, fext = os.path.splitext(file)
if fext == '.c':
readOutput(testdir + fname + fext)
break