-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?

6
  • 3
    This smells like an XY problem. What are you actually trying to achieve? Commented Jan 22, 2017 at 19:27
  • What does utils.fileReader() do? Why aren't you importing it? Commented Jan 22, 2017 at 19:43
  • I'm sending information over the socket and have to break it down into a list and then re assemble it into code. there is a valid reason for doing this, its not as simple as importing it. Commented Jan 22, 2017 at 19:51
  • file reader is a method i created to read files Commented Jan 22, 2017 at 19:52
  • Does python3 <some Python code> usually work? Commented Jan 22, 2017 at 19:58

2 Answers 2

2
import holderFile 

Or:

from holderFile import simpleAdder 

And then call simpleAdder normally.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm sending information over the socket and have to break it down into a list and then re assemble it into code
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.