Python Forum
passing php variable to python file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
passing php variable to python file
#1
I have a php file that pass file name as string variable to python. And once the python receive the string variable it will open the file based on the string variable.

python file
#!C:\Python\Python39\python.exe
print ('Content-type: text/html\n\n')
import sys
print(sys.argv)
filename = sys.argv[1]
print(filename)

with open(filename+'.py', 'r') as inFile:
  print(inFile.read())
php file
$filename = "stringfile";
$command = "python receivephpvariable.py  \"$filename\"";
echo $command ;
 echo shell_exec($command);
but i got the error :
Error:
['c:\\xampp\\htdocs\\project_real\\receivephpvariable.py'] Traceback (most recent call last): File "c:\xampp\htdocs\project_real\receivephpvariable.py", line 5, in <module> filename = sys.argv[1] IndexError: list index out of range
Reply
#2
I haven't used PHP for quite a while, but don't you need to index line 4 of the php to return first value?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 359 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 730 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Passing flags to python script, through a function xbit 4 3,971 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  Python reading variable in another py file wrongly _vertig0 2 1,967 Nov-21-2020, 07:19 AM
Last Post: _vertig0
  Passing List of Objects in Command Line Python usman 7 3,168 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  passing variable to function Rejoice 4 2,870 Sep-11-2020, 03:27 AM
Last Post: Pleiades
  Python 2.7 passing variables from functions zetto33 1 1,778 Mar-19-2020, 07:27 PM
Last Post: Larz60+
  Passing variable to another function JonnyDriller 10 3,776 Feb-05-2020, 03:46 AM
Last Post: JonnyDriller
  Passing a local variable outside of a function malonn 8 3,928 Apr-15-2019, 01:50 AM
Last Post: malonn
  Passing parameters from C# to Python[for using in gensim] in Visual Studio 2017 ZoghbyAya 0 3,861 May-21-2018, 08:49 PM
Last Post: ZoghbyAya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020