2

I have a php code that is writing the user input on the webpage into a text file. I wish to pass the text file into my python script that looks like follows:

PHP Script (getfile.php)

<?php

function writetofile($file, $content, $writeType){
    $fo = fopen($file, $writeType);
    if($fo){
        fwrite($fo,$content);
        fclose($fo);
    }   
}
?>

Python Script (predict.py)

clf=joblib.load('model.pkl')

def run(command):
    output = subprocess.check_output(command, shell=True)
    return output

row = run('cat '+'/Users/minks/Documents/X-test.txt'+" | wc -l").split()[0]
print("Test row size:")
print(row)
matrix_tmp_test = np.zeros((int(row),col), dtype=np.int64)
print("Test matrix size:")
print(matrix_tmp_test.size)

What I am asking is, after writing to a file : $file in php, how can I then pass this file to replace:

 row = run('cat '+'/Users/minks/Documents/X-test.txt'+" | wc -l").split()[0]

where the path gets replace by $file and the processing continues? Also, is it possible to pass $file directly to the python code via command line? I am little confused on how this entire passing and processing can be carried out.

2
  • sys.argv[1] in python and sun a system exec command on your script and pass the file path as an argument. Commented Feb 19, 2016 at 12:10
  • @Torxed: But how do l ensure that the file automatically goes and fits inside that particular 'row' statement? There are other file calls inside the python code as well Commented Feb 19, 2016 at 12:12

1 Answer 1

1

Dow you want something like this?

PHP:

$path = "my.txt";
system("python predict.py $path");

Python:

row = run("cat %s | wc -l" % sys.argv[1]).split()[0]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.