python - Execute a file in temp (tempfile) -
i'm trying write python script creates temporary file (another python script) in temp folder. should proceed execute temp file.
i wrote this:
tempfile.temporaryfile(mode='w+b', suffix='.py') temp: stuff stuff stuff temp.write(mycode.encode('utf-8')) temp.seek(0) os.system(temp.name)
when run it, the
the process cannot access file because being used process.
error. tried use tempfile.mkstemp instead (hoping "close" function handle this), nothing changes.
so question is: how can create file, python script, in temp folder, execute it, , delete it?
thanks answers :d
the line os.system(temp.name)
should outside of with
closure.
doing doesn't work here since file removed then, therefore need have parameter delete=false
passed temporaryfile()
.
preferably consider using namedtemporaryfile.
Comments
Post a Comment