0

I would like to call a Python file from within PHP. I've tried this :

exec('C:\Python34\python.exe ./python/genereBonDeCommande.py');

But it doesn't work. In my Python file I've got this :

fichier = open(repertoire+"C:/Program Files/EasyPHP-DevServer-14.1VC9/data/localweb/projects/Administration/bonsDeCommandes" + nom_fichier, "No13");
fichier.write('hello');

Then how can I call a Python file from within PHP?

Dakkaron
  • 5,930
  • 2
  • 36
  • 51
Saroten
  • 295
  • 1
  • 5
  • 16
  • Below link may help you. http://stackoverflow.com/questions/19735250/running-a-python-script-from-php – deepu Jun 08 '15 at 09:13
  • How to check if it did call the file ? @deepu – Saroten Jun 08 '15 at 09:20
  • possible duplicate of [Running python script from php](http://stackoverflow.com/questions/10937487/running-python-script-from-php) – TZHX Jun 08 '15 at 09:20

2 Answers2

0

Try executing your script with the full path to your file like this:

<php
//If your file is in C:\wamp\www\python
exec('C:\Python34\python.exe C:\wamp\www\python\genereBonDeCommande.py');
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
  • It tells me "io.UnsupportedOperation: not writable" when i execute the file with the cmd – Saroten Jun 08 '15 at 10:08
  • can you just echoes a value in your genereBonDeCommande.py to test where is the problem? Because maybe the error is in this file – Adrian Cid Almaguer Jun 08 '15 at 10:11
  • The problem is that it doesn't want to write into the file. If i put fichier.read() instead it works properly. – Saroten Jun 08 '15 at 10:12
  • @guillaumedrillaud then your question *"Then how can I call a Python file from within PHP?"* is solved the problem is other – Adrian Cid Almaguer Jun 08 '15 at 10:17
  • I don't know if it works because if i put fichier.read() there is no error but i don't know if the call worked or not. How can I check if the call worked ? – Saroten Jun 08 '15 at 10:20
  • @guillaumedrillaud just put in your file *print("Hello World")* and *echo exec('C:\Python34\python.exe C:\wamp\www\python\genereBonDeCommande.py');* and see the result – Adrian Cid Almaguer Jun 08 '15 at 10:23
0

Here is the solution, to create a file there is to put this line :

fichier = open("C:/Program Files/EasyPHP-DevServer-14.1VC9/data/localweb/projects/Administration/bonsDeCommandes/No15.txt","w");

w creates a file if it doesn't exist already and write over if it does exist

instead of this one :

fichier = open(repertoire+"C:/Program Files/EasyPHP-DevServer-14.1VC9/data/localweb/projects/Administration/bonsDeCommandes" + nom_fichier, "No13");
Saroten
  • 295
  • 1
  • 5
  • 16