8 ответов
+ 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))
+ 8
Try this:
exec(open("file_name.py").read())
+ 7
Try it here:
https://code.sololearn.com/c5f76DBYhYSO/
+ 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 :)
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
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()
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,,.
0
yes, it works in browser :-) , not in SoloLearn app for Android ;-(
P.s.: what is the [PythonEdu]?