Search code examples
pythonshellsubshell

How to execute Python files from Python?


I am attempting to execute Python files from a deconstructed file.

import utils
import os
print(utils.fileReader('holderFile.py'))
test = utils.fileReader('holderFile.py')
for i in test:
    if(i == ''):
        os.system('')  #this allows for it to read spaces in the file
    else:
        os.system('python3 ' + i)
        print(i)
os.system('python3 exit()')
#os.system("sudo python scale1.py")


print('Done')

It is running but it gives me this error

sh: 1: Syntax error: "(" unexpected
def simpleAdder(i, j):
sh: 1: Syntax error: "(" unexpected
    return (i+j)
sh: 1: Syntax error: "(" unexpected
simpleAdder(5, 8)
sh: 1: Syntax error: "(" unexpected

holderFile.py is just a simple addition method

def simpleAdder(i, j):
    return (i+j)

simpleAdder(5, 8)

How would I go about getting a Python file to execute properly using a method similar to this, or what would you suggest I use?


Solution

  • import holderFile
    

    Or:

    from holderFile import simpleAdder
    

    And then call simpleAdder normally.