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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -