简体   繁体   中英

Django Python Script Doesn't Output Files

I have a script that executes the following code through a Django view:

# generate dag file
        try:
            commandString = [
                'python',
                os.path.join('/srv/nfsshare/transcode50', userFolder, directory, 'condor_execute.py')
            ]
            subprocess.check_call(commandString,
                            stdout=open('/srv/nfsshare/transcode50/output2.txt', 'w'),
                            stderr=subprocess.STDOUT)
        except Exception, e:
            open('/tmp/test_exception.txt', 'w').write(str(e))
            raise

What that is supposed to do is execute a Python file I have generated. If I run that exact command from the server's command prompt (ie, python /srv/nfsshare/transcode50/cogden/algorithms/condor_execute.py ), it works just fine and produces the needed files it's supposed to within that directory. However, if I run it from Django, it produces no error messages, produces the correct console output (a message that basically says "Created file blah at blah"), but produces no files whatsoever, when it normally generates two. I'm getting no permissions errors or anything and have ensured the directory is chmodded appropriately.

I suspect your process is never getting around to closing the file (as you can't do it explicitly as you have no reference to it - while run as a script - the interpreter would have stopped - but Django works different with loaded processes etc...) and it's not big enough to be flushed - try moving out your open .

with open('/srv/nfsshare/transcode50/output2.txt', 'w') as stdout:   
    subprocess.check_call(commandString, stdout=stdout, stderr=subprocess.STDOUT)

Also, make sure that when you make changes to make sure the Django server is restarted/similar to make sure code is reloaded and changes are taken into effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM