Subprocess.Popen error - python

Proc = subprocess.Popen ([ 'FileName'])
The FileName is a variable which stores "/home/USER/exec.sh &", the program searches for the exec.sh file in the home folder and stores the path in FileName.I am unable to start this exec.sh process.It gives me the following error
OSError: [Errno 2] No such file or directory
I initially used::
os.system(FileName)
It worked perfectly but didn't return the pid. Thus, I switched to Popen.

just:
fileName = "/home/USER/exec.sh"
proc = subprocess.Popen(fileName)
pid = proc.pid

Related

why does subprocess.runI(['mdfile', filename], capture_output = True).stdout return an empty string?

When I run midi.open('next.midi') from the code
import subprocess
class midi:
def __init__(self, contents):
self.contents = contents
def open(filename):
subprocess.run(['mv', str(subprocess.run(
['mdfind', filename], capture_output = True).stdout).split(
'\n')[0][2:-3], filename.split(
'.')[0] + '.txt'], capture_output = True)
file = open(filename.split('.')[0] + '.txt')
contents = file.readlines()
file.close()
midi(contents)
I get the error message FileNotFoundError: [Errno 2] No such file or directory: 'next.txt'
after some digging, I found that subprocess.run(['mdfind', filename], capture_output = True).stdout returned an empty string when it should be returning the path. What am I doing wrong?
bcause you have to chage working dir of program try this
import os
after that do
# add this before subprocess.run
os.chdir(directory here)
for example your file is in a directory C:\temp\
you cannot access it directly, because it is in an other directory, therefore to access that file you has to change your directory
to that directory by os.chdir('C:\temp\') it will change
the directory to the directory in which that file is stored

Python FileNotFoundError: [Errno 2] despite giving full filepath

So I have written a piece of code which first runs a powershell command to generate a UTF-8 version of a DAT file (have been having special character issues with the original file, hence the step). Following which I try to open the newly created file. But the issue is, I keep getting 'FileNotFoundError: [Errno 2]' Initially I was only trying with the file name since the newly created file was in the same folder, but then i tried to generate the absolute path as well.
import os
import subprocess
subprocess.Popen('powershell.exe -Command "Get-Content .\Own.DAT | Set-Content -Encoding utf8 Own1.dat"')
filepath = __file__
filepath = filepath[:-7]
with open(filepath+"Own1.dat", "r") as f:
I can confirm that filepath+"Own1.dat" is fetching the correct filepath. Yet can't figure out what the issue could be.
Edit: Someone asked for confirmation, here is the message i am getting:
C:\Users\Debojit\MiniConda3\python.exe "E:/My Documents/Work/essbase/ownership/test.py"
Traceback (most recent call last):
File "E:/My Documents/Work/essbase/ownership/test.py", line 18, in <module>
with open(filepath+"Own1.dat", "r") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'E:/My Documents/Work/essbase/ownership/Own1.dat'
Process finished with exit code 1
Note: Curiously enough if i put the powershell command into a separate batch file, write a code in the python script to run it, the works without any issues. Here is the code i am talking about:
import os
import subprocess
from subprocess import Popen
p = Popen("conversion.bat", cwd=r"E:\My Documents\Work\essbase\ownership")
stdout, stderr = p.communicate()
filepath = __file__
filepath = filepath[:-7]
with open(filepath+"Own1.dat", "r") as f:
The conversion.bat file contains the following
powershell.exe -Command "Get-Content .\Own.DAT | Set-Content -Encoding utf8 Own1.DAT"
But I don't want to include a separate batch file to go with the python script.
Any idea what might be causing the issue?
Your error is unrelated to powershell. Popen runs asynchronously. In one command, you are using communicate(), but in the other, you are not.
You're using Popen() incorrectly.
If you want run a command and also pass arguments to it, you have to pass them as a list, like so:
subprocess.Popen(['powershell.exe', '-Command', ...])
In your code, popen tries to run a command literally named powershell.exe -Command "Get-Content ... which of course doesn't exist.
To use a simpler example, this code won't work:
subprocess.Popen('ls -l')
because it's trying to run a command literally named ls -l.
But this does work:
subprocess.Popen(['ls', '-l'])
I still couldn't figure out why the error was happening. But I found a workaround
with open("conversion.bat","w") as f:
f.writelines("powershell.exe -Command \"Get-Content '" +fileName+ "' | Set-Content -Encoding utf8 Own1.dat\"")
from subprocess import Popen
p = Popen("conversion.bat", cwd=os.path.dirname(os.path.realpath(__file__)))
stdout, stderr = p.communicate()
os.remove("conversion.bat")
Basically I would create the batch file, run it and then delete it once the file has been created. Don't why I have to use this route, but it works.

Pexpect: flushing output of spawn object to disk immediately

I have this code snippet to test zip archive integrity and writing the output to a file (stdout and stderr):
cmd = "gunzip -t " + crashFile + " > err.txt 2>&1"
p.sendline(cmd)
p.expect('\$ ')
f = open("err.txt")
However it always fails to open the file with the following error:
f = open("err.txt")
IOError: [Errno 2] No such file or directory:'err.txt'
But the file does exist. So it looks like the gunzip runs but the system isn't flushing the output to disk "in time" for the open to read the file.
Any ideas?

Error when creating directory and then opening a file within it in Python, but no error if directory already exists

I expect a directory to be created and then a file to be opened within it for writing to when I execute my code below in Python 2.6.6,
import subprocess
def create_output_dir(work_dir):
output_dir = '/work/m/maxwell9/some_name5/'
subprocess.Popen(['mkdir', output_dir])
return output_dir
if __name__ == '__main__':
work_dir = '/work/m/maxwell9/'
output_dir = create_output_dir(work_dir)
#output_dir = '/work/m/maxwell9/some_name5/'
filename = output_dir + 'bt.sh'
with open(filename, 'w') as script:
print('there')
but instead I get the error,
Traceback (most recent call last):
File "slurm_test.py", line 13, in <module>
with open(filename, 'w') as script:
IOError: [Errno 2] No such file or directory: '/work/m/maxwell9/some_name5/bt.sh'
If I run the script, I can then see that the directory is created. If I then uncomment the line,
#output_dir = '/work/m/maxwell9/some_name5/'
and comment the line,
output_dir = create_output_dir(work_dir)
then the file is output fine. So there is something about creating the folder and then writing to it in the same script that is causing an error.
subprocess.Popen starts up an external process but doesn't wait for it to complete unless you tell it to (e.g. by calling .wait on the returned Popen instance). Most likely, mkdir is in the process of creating a directory while open(filename, 'w') attempts to create a file in that directory. This is an example of a "race condition".
The solution is to .wait on the open process (as noted above), or you can use one of the convenience wrappers subprocess.check_output, subprocess.check_call or (even better), you can avoid subprocess entirely by using os.mkdir or os.makedirs.
You could use the os library instead of subprocess, which makes for a more straightforward implementation. Try swapping out your create_output_dir function with this:
import os
def create_output_dir(work_dir):
try:
os.makedirs(work_dir)
except OSError:
pass
return work_dir

"No such file or directory" sp.Popen module

I am trying to take a video and convert it in audio , for this I am using ffmpeg in python. I run the following command but it gives me " No such file or directory" for the input file. Here's the code-
FFMPEG_BIN = "ffmpeg"
import subprocess as sp
command = [FFMPEG_BIN, '-i', '/home/suryansh/Downloads/t.mp4']
pipe = sp.Popen(command, stdout = sp.PIPE)
After executing this code I get home/suryansh/Downloads/t.mp4: No such file or directory but the file is their in the path specified.
try add
shell=True
after the "command", and i think that you could guide for the next example
https://stackoverflow.com/a/26312166/4941927

Categories

Resources