Puma
Puma

Reputation: 1

Running PHP script using python

I'm trying to run this script using the code

subprocess.call(["php  C:\Python27\a.php"])

and I'm getting this error:

FileNotFoundError: [WinError 2] The system cannot find the file specified

i have tried changing path but nothing seems to work, any ideas?

Upvotes: 0

Views: 68

Answers (2)

ʇsәɹoɈ
ʇsәɹoɈ

Reputation: 23509

Try this:

subprocess.call(["php",  "C:\\Python27\\a.php"])

From the documentation:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

Also note that in python, like many other languages, the backslash in a normal string has special meaning. You'll have to use double backslashes or a raw string to get the behavior you want.

Upvotes: 1

BSasuke
BSasuke

Reputation: 79

Either

subprocess.call(["php",  "C:\\Python27\\a.php"])

or

subprocess.call(["php",  r"C:\Python27\a.php"])

should work.

Upvotes: 1

Related Questions