My Problem now is, that the
stdout=open(path,"w")
seems to be sort of corrupt, because I can't access or even delete it, but I can see it in the folder.
The issue is that path = f.readline()
return a string with a newline at the end. open(path, 'w')
on some systems doesn't care and creates the file with a newline at the end. Try print(os.listdir('.'))
to see \n
in the filename.
To fix it, just remove the newline: path = path.strip()
.
There are other unrelated issues in your code:
you are mixing tabs and spaces for indentation. Never do that: it makes the visual indentation different from the one Python sees as it currently is in your question. Either always use tabs or spaces, not both. You can configure your editor to insert 4 spaces when you hit Tab key.
you probably meant
cpuLoad = psutil.cpu_percent()
instead ofcpuLoad = cpuLoad + psutil.cpu_percent()
that increases each iteration of the loop for no reasonremove the newline from the command and remove
shell=True
. Don't use shell unless it is necessary (it is a good habit that sometimes may be broken if you know what you are doing):import shlex process = Popen(shlex.split(command), stdout=output_file, stderr=stderr_file)
use
with
-statements for the code to be compatible with other Python implementations such as Jython, Pypy:with open(path, 'w') as output_file: with open('error.log', 'w') as stderr_file: process = Popen(shlex.split(command), stdout=output_file, stderr=stderr_file)
otherwise the files may remain open in the parent process.
remove
f.close()
,myfile.close()
after thewith
-statement that closes the file by itself even if an exception occurs that is its purpose, its raison d'être..close()
is harmless but pointless in this caseuse
if not first_line.strip():
to test whether the first line is blank (contains only whitespace)the whole idea of manually editing
JobQueue.txt
is fragile. Nothing stops Python process to read only partial input. You could use a dedicated command to add new jobs e.g.,at
-like utility. And implement it however you like e.g., listen on a port for a new jobs in the main script and send jobs inat
-like utility. Here's a code example of a the very basic socket client and server in Python. Or use other IPC methods.