shell_exec will only return what get's printed to stdout. If an exception arises in the Python script, the whole exception message and traceback are printed on stderr, so they won't be part of the output. Note that theoretically,
shell_exec should return NULL if an error happens but well, this is PHP so it wouldn't be much fun if the builtin funcs behaved as documented...
Anyway: the problem is very probably with opening the file, and is very probably a permission issue.
First, you're using a relative path, and relative path are resolved against the current working directory, not against the directory where your script is located, so if you expect a sane and predictable behaviour always use an absolute path (either pass it as argument or build it against the script's current location).
Also your Apache server may not (this is an understatement) run with the same env and user as you so it might just not be allowed to open a file for wrting in whatever-his-own-current-working-directory is at that time.
And finally, even it's PYTHONPATH might be different than yours so it might even be executing another (leftover)
test.py OR a stale
To make a long story short: it's impossible to tell for sure what's wrong without having access to your system, but at least now you know where to start debugging.
The first thing I'd personnaly do would be to make sure any error in the python script is printed to stdout:
import sys print "argv1: ", sys.argv print "argv2: ", sys.argv print "argv3: ", sys.argv try: fname = "test-%s.txt" % sys.argv with open(fname,"w+") as f: f.write("test") print "Text added." except Exception as e: print "oops : got %s" % e raise