Take the 2-minute tour ×

The following code works fine in Windows:

subprocess.Popen([PATH_TO_G++]/g++ file.cpp -o file.exe)

However in Linux I get the following error:

OSError: [Errno 2] No such file or directory

After reading the documentation and several SO threads, I found out that subprocess.Popen works differently in Windows and nix systems. In windows it takes the string as the parameter and launches it just like you'd launch it in terminal.

In linux however it requires a list of strings if you have parameters. The first value is the program itself, then go the attributes. You can make it behave like Windows by passing the Shell=True argument, but that's not a good solution for me.

I tried the shlex.split function, but it still doesn't work.

share|improve this question
Have you tried `subprocess.Popen(["progpath", "arg1", "arg2", "arg3"])? –  khachik Apr 13 '11 at 13:08
@khachik, yes, but I might have gotten them wrong. How to use the arguments with my example? –  Alex Apr 13 '11 at 13:11
you can pass in params as a sequence on all platforms. what's your actual question? –  Corey Goldberg Apr 13 '11 at 13:11
your example doesn't make sense, because it is invalid syntax. Could you please provide the code which causes "No such file or directory"? –  khachik Apr 13 '11 at 13:12
add comment

1 Answer

up vote 4 down vote accepted

Based on your example you'll need to make sure the command is properly quoted:

subprocess.Popen([PATH_TO_G++ + "/g++", "file.cpp", "-o", "file.exe"])

There is no way PATH_TO_G++ is a valid variable name, so I'm just going to assume that you provided that as an example.

Now, more importantly, what are you trying to do with the subprocess? Just launch it and have it be the primary operation? Launch it and capture the output? Launch it in the background?

The documentation for the subprocess module is pretty clear and provides a lot of examples on how you might use it.

share|improve this answer
add comment

Your Answer


