+ 4

Is there a method to call another python file for execution. Not impporting, something like system() in PHP or execv() in C++ ?

Calling outer python code.

21st Mar 2017, 4:59 AM
learner
8 odpowiedzi
+ 9
Try this below. Works under SoloLearn. Would you mind if I added it to the [PythonEdu] collection? import os fn="my.py" str="print (12+2)\nf=open('m3.py','w')\nf.write(\"print(\'hello world\')\")\nf.close()" fp=open(fn, 'w') fp.write(str) fp.close() path='.' print(os.listdir(path)) exec(open("my.py").read()) print(os.listdir(path))
21st Mar 2017, 7:39 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 8
Try this: exec(open("file_name.py").read())
21st Mar 2017, 6:52 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
21st Mar 2017, 7:55 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
Weird... works in my app alright. PythonEdu is a microproject started recently on SoloLearn. I put codes that focus on certain interesting aspects which are barely or not at all touched on SoloLearn. This one I find interesting :)
21st Mar 2017, 8:31 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
import os fn="my.py" str="print (12+2)\nf=open('m3.py','w')\nf.write('print(\'hello world\')')\nf.close()" fp=open(fn, 'w') fp.write(str) fp.close path='.' print(os.listdir(path)) exec(open("my.py").read()) #Output: # ['my.py', 'source.py'] # file m3.py not appear
21st Mar 2017, 7:09 AM
learner
0
In my code console.py there is a bug if input: p=2 quit() error on '=' one way to debug is drop input to file my.py and exec it ------------------- """ file: console.py input: print(2**5) print(cmd_list) print(list(map(lambda x:print(x), (x for x in cmd_list)))) quit() """ def console(): last_cmd = "" cmd_list = [] cmd = "" res=0 while True: last_cmd = cmd cmd = input("Enter line of Python's code or quit() to ends: " ) if cmd=="quit()": break else: res=eval (cmd ) cmd_list.append(cmd ) print ([cmd_list,res]) return cmd_list console()
21st Mar 2017, 7:15 AM
learner
0
... print(os.listdir(path)) exec(open("my.py").read()) print(os.listdir(path)) #Output: # ['my.py', 'source.py'] # ['my.py', 'source.py'] # file m3.py still not exist P.s.:; if or when this code would work,,.
21st Mar 2017, 7:50 AM
learner
0
yes, it works in browser :-) , not in SoloLearn app for Android ;-( P.s.: what is the [PythonEdu]?
21st Mar 2017, 8:17 AM
learner