-1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Marc Frame
  • 923
  • 1
  • 13
  • 26

2 Answers2

3
import holderFile

Or:

from holderFile import simpleAdder

And then call simpleAdder normally.

Trelzevir
  • 767
  • 6
  • 11
  • I'm sending information over the socket and have to break it down into a list and then re assemble it into code – Marc Frame Jan 22 '17 at 19:49
0

I figured it out and will leave the thread open for others to find an answer in case they ever need it.

I simply saved the file to a temp file and then ran the file instead of attempting to run individual lines

def fileWriter(array, file):
    f = open(file, 'w+')
    for item in array:
        f.write("%s\n" % item)

os.system('python3 temp.py')

works for me

Marc Frame
  • 923
  • 1
  • 13
  • 26