Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I need to execute a python script that writes a file in the same directory.

test.py :

print 'Hi! I was executed'

test2.py :

filename = 'sample.txt'
target = open(filename,'a')
target.write("Something Something")
target.close()

Php script:

<?
    exec('python test.py',$output1,$ret1);
    exec('python test2.py',$output2,$ret2);
?>

The first exec works fine but the second script does not, the return var $ret2 is 1. Both the commands work perfectly fine in the terminal. I guess it is a permission issue as php scripts are executed as 'nobody'.

Thanks in advance

share|improve this question
1  
try to give full path for filename = 'sample.txt' – SajithNair Mar 6 '14 at 7:05
    
like filename = '/home/pranjal/sample.txt' – SajithNair Mar 6 '14 at 7:05
    
Yep, works fine now. Thanks! – Pranjal Mar 6 '14 at 7:08
    
Ok I will add this as an answer then – SajithNair Mar 6 '14 at 7:13
    
@SajithNair I have another question here stackoverflow.com/questions/22230525/… – Pranjal Mar 7 '14 at 5:45

2 Answers 2

up vote 0 down vote accepted

try to give full path for filename = 'sample.txt'

For e.g.

filename = '/home/pranjal/sample.txt'
share|improve this answer

I would consider using a COM:

<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("[Your command here]", 0, false); 
?>

Mind you this will not work wonders running in a Linux server.

Also please consider that you are allowing your script to execute code, please make sure it's all safe when it needs to be :)

escapeshellarg()
escapeshellcmd() 

Will remove the commands that you don't want to run.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.