0

I am trying to make python program to score the homework written in c++. My laptop os is window so i installed g++ in order to use subprocess command. This is my code.

Python code:

import subprocess
import os
import glob
from openpyxl import Workbook



path='C:\\auto_scoring\\'
def write_csv(tc_num,ans):
    score_book=Workbook()

def score(testcases):#scroing function
    correct=True
    for i,testcase in enumerate(testcases):
        if correct:
            print(i+1,":correct")
        else :
            print(i+1,":wrong")


def exe_maker(filename):
    ret=filename.rstrip('.cpp')+'.exe'
    return ret

def execute(file):
    ret=subprocess.check_output('check')


def get_testcase():
    global path
    text_name='test.txt'
    r=open(text_name,mode='r',encoding='utf8')
    text=r.read()
    print(text)

def run():
    file_list = glob.glob('*.cpp*')#cpp

    for file in file_list:
        exe_file=exe_maker(file)
        command='g++ -o '+path+exe_file+" "+path+file
        print(command)
        subprocess.call(command)
        print("start!")
        ret=subprocess.check_output(file.rstrip('.cpp')+" 1").decode('ascii')
        print(ret)


def main():

    run()

def exp():
    ret=subprocess.check_output('check').decode('ascii')

    if ret=='1':
        print('correct')
    else:
        print('wrong')



if __name__=='__main__':
    main()

But I have to use pass the parameter such as C++ example:

int main(int argc,char*argv){

    cout<<argv[1];
}

I don't want to use this kind of thing. Program without int argc and char argv. I just want to check whether the answer is correct. For example) input: My name is k -> output: Hello K. If i couldn't make the program , I have to run every 100 c++ files... I really need help. thank you.

cookiecat
  • 19
  • 2

1 Answers1

0

Try using subprocess.run, where you pass each item in your command line as a list:

command=['g++', '-o' , exe_file, path + file]
subprocess.call(command)
Brainless
  • 1,522
  • 1
  • 16
  • 30
  • thanks for the comment! What is the difference between call and run? And I am curious that is there any way to passing parameter without using C++ file's main(int argc,char*argv)?? – cookiecat Mar 16 '20 at 12:42
  • https://stackoverflow.com/questions/40697583/whats-the-difference-between-pythons-subprocess-call-and-subprocess-run – Brainless Mar 16 '20 at 12:48
  • "And I am curious that is there any way to passing parameter without using C++ file's main(int argc,char*argv)?" I'm afraid I don't understand... What do you mean? – Brainless Mar 16 '20 at 12:48
  • Awesome, I'm glad! feel free to upvote and/or accept the answer – Brainless Mar 18 '20 at 10:59