Six Stars

how to execute a python script file with an argument using tSystem

Hello, 

i created this job tFileList------->tSystem------------>tExtractDelimetedFields

in order to iterates on files and folders with tFileList, get ((String)globalMap.get("tFileList_1__CURRENT_FILEPATH")) as result and use this result in tSytem as argument to my script Python.

rq : my script python  allow me to read DBF file.

I used this command : "python \"liredbf.py" + ((String)globalMap.get("tFileList_1__CURRENT_FILEPATH")) but i got this error 

python: can't open file 'liredbf.py null': [Errno 2] No such file or directory 

How can i get the result of tFileList to use it as argument for my Script Python.

 

Thank you in advance
cordially.

  • Data Integration
1 ACCEPTED SOLUTION

Accepted Solutions
Nine Stars

Re: how to execute a python script file with an argument using tSystem

ok, and on original post, and on picture - I saw wrong name of variable

 

correct - 
((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"))

on picture and in original post -
((String)globalMap.get("tFileList_1__CURRENT_FILEPATH"))

 please double-check

 

it also my mistake, because I copy past from Your post, but this is could be reason for NullPointer error

-----------
13 REPLIES
Nine Stars

Re: how to execute a python script file with an argument using tSystem

generally You can test:

 

 

"python \"liredbf.py\" " + ((String)globalMap.get("tFileList_1__CURRENT_FILEPATH")) 

look for close \"and space after liredbf.py 

 

 

or You can switch mode of tSystem to Use Array and pass each parameter as separate line

Screen Shot 2017-05-11 at 9.46.17 AM.png

also You can omit open and close \" (not sure, but usually they need only if filename with space)

-----------
Six Stars

Re: how to execute a python script file with an argument using tSystem

Thanks vapukov for your response. 

I try to do the second solution, i don't get an error but also i don't get any result. 

context.PathPython is: "python \"C:\\Users\\Ines\\Desktop\\Python\\lirealldbf.py"

context.var is : ((String)globalMap.get("tFileList_1__CURRENT_FILEPATH"))

i do this test c1.PNGc2.PNG

I think my script python can't read this path ((String)globalMap.get("tFileList_1__CURRENT_FILEPATH")) so it can't open the file ! 

 

Nine Stars

Re: how to execute a python script file with an argument using tSystem

it wrong construction

 

try this - first without any context variables 

row by row

 

1) "python"
2) "C:\\Users\\Ines\\Desktop\\Python\\lirealldbf.py"
3) ((String)globalMap.get("tFileList_1__CURRENT_FILEPATH"))

 

or 

1) "cmd"
2) "/c"
3) "python" 4) "C:\\Users\\Ines\\Desktop\\Python\\lirealldbf.py" 5) ((String)globalMap.get("tFileList_1__CURRENT_FILEPATH"))

I do not have access today for windows Talend Studio and not remember it accept first variant or not (on mac I use 1st)

 

also - check is python in path or not - when windows start new process for tSystem, it is not fact all PATH variables will be passed, so try to add full path to Python

 

-----------
Six Stars

Re: how to execute a python script file with an argument using tSystem

I've changed my construction but i got this error :

Exception in component tSystem_1
java.lang.NullPointerException
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at local_project.secondtest_0_1.SecondTest.tFileList_1Process(SecondTest.java:1097)
at local_project.secondtest_0_1.SecondTest.runJobInTOS(SecondTest.java:2284)
at local_project.secondtest_0_1.SecondTest.main(SecondTest.java:2132)
2017-05-11 10:42:22|Kc8fjP|Kc8fjP|Kc8fjP|LOCAL_PROJECT|SecondTest|Default|6|Java Exception|tSystem_1|java.lang.NullPointerException:null|1
[statistics] disconnected 

 

In my code Python i wrote this path f1 =sys.argv[1] to read the file as an argument and i got a result when i execute my script python on command ligne but with talend doesn't work ! 

 

Nine Stars

Re: how to execute a python script file with an argument using tSystem

show the current pictures

-----------
Six Stars

Re: how to execute a python script file with an argument using tSystem

Here is the picture 

c3.PNG

Nine Stars

Re: how to execute a python script file with an argument using tSystem

please add - what in tSystem settings?

-----------
Six Stars

Re: how to execute a python script file with an argument using tSystem

c4.PNG

And i tried this version but usually the same error 

1) "cmd"
2) "/c"
3) "python" 4) "C:\\Users\\Ines\\Desktop\\Python\\liredbf.py" 5) ((String)globalMap.get("tFileList_1__CURRENT_FILEPATH"))

 

Nine Stars

Re: how to execute a python script file with an argument using tSystem

ok, and on original post, and on picture - I saw wrong name of variable

 

correct - 
((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"))

on picture and in original post -
((String)globalMap.get("tFileList_1__CURRENT_FILEPATH"))

 please double-check

 

it also my mistake, because I copy past from Your post, but this is could be reason for NullPointer error

-----------
Six Stars

Re: how to execute a python script file with an argument using tSystem

It is true, that's the error thank you very much Smiley Happy

Now my job can read the file but it read only one ! 

I don't now why !

c6.PNG

Nine Stars

Re: how to execute a python script file with an argument using tSystem

it just wrong screen picture

because

left - number of iterations
right - number of files in current iteration :-) and it always 1

-----------
Six Stars

Re: how to execute a python script file with an argument using tSystem

Ah ok Smiley Happy

Thank you very much for your help.

It works.

Nine Stars

Re: how to execute a python script file with an argument using tSystem

Welcome :-)

-----------