1

I have 2 scripts. One is .bat and other is python. The python script is triggered by the .bat file. While executing, first I will run .bat with command line arguments but then I need to read the argument into the python script.

What am I doing wrong?

I call the batch script like this:

C:>main.bat c:\temp\text1.txt

main.bat :

@ECHO off
set var1=%~1
call python_script.bat 
echo "returned to main"
pause

python_script.bat :

python -x %0 %*

print var1        # Notworking

import sys
var1 = sys.argv  ############  Also not working

with open(var1, 'r+') as f:
content = f.read()
f.seek(0)
f.trunca........
wpercy
  • 9,636
  • 4
  • 33
  • 45
Running Forward
  • 90
  • 1
  • 1
  • 11
  • `sys.argv` is a list. The first element is the name of the script itself, the second element is the first command line argument. Why don't you simply put your python in a script_name.py file and call python script_name.py from your .bat file? Related question: https://stackoverflow.com/questions/1013246/call-python-from-bat-file-and-get-return-code – jDo Mar 08 '16 at 15:10
  • I have 2 more scripts to call... and , for all i need the command line argument to the python... and bat scripts. the first trigger will be with .bat. is this really possible?? – Running Forward Mar 08 '16 at 15:11
  • but, from main.bat to python_script.bat do no pass the var1, are two "instances" – Fabio Duran Verdugo Mar 08 '16 at 15:18
  • Aren't the scripts working with the same file? (c:\temp\text1.txt) In that case, couldn't you simply hardcode the command line arguments to the python script in your bat file instead of trying to pass args from bat to py? I'm not on windows though so I can't really test it. – jDo Mar 08 '16 at 15:18
  • No.. actually, (c:\temp\text1.txt) is an example. user dont know this path. the path will be given along with .bat file as command line argument. and that python needs to read it – Running Forward Mar 08 '16 at 15:20
  • Calling python script from bat. Maybe useful: https://stackoverflow.com/questions/13880650/how-to-call-run-multiple-python-scripts-from-batch-file-in-window-xp-7 – jDo Mar 08 '16 at 15:23
  • gone through that link. calling .py from bat is not the issue i need that command line argument as a variable in both the script.. :( – Running Forward Mar 08 '16 at 15:26

1 Answers1

0

I don't know much about bat and windows but doesn't windows support calling a python script with a command line argument from a bat file? If so, wouldn't something like this work?

This works in Linux shell:

call_py.sh:

# do stuff with command line argument 1 here ($1) and pass it on to py
echo "I'm a shell script and i received this cmd line arg: " $1
# pass $1 on to python as a cmd line arg here
python some_script.py $1
echo "shell script still running after python script finished"

The other question I linked to showed us how to call python from bat (although I can't verify it). Couldn't you simply add you var1 after the name of the py script like I did in call_py.sh?

# syntax might be wrong
start C:\python27\python.exe D:\py\some_script.py var1

some_script.py then receives $1/var1 as sys.argv[1]

some_script.py:

import sys

print "I'm a python script called " + sys.argv[0]
print "I received this cmd line arg from shell: " + sys.argv[1]

Output:

$ sh call_py.sh "VARIABLE_GIVEN_TO_SHELL_AND_PASSED_TO_PY"
I'm a shell script and i received this cmd line arg:  VARIABLE_GIVEN_TO_SHELL_AND_PASSED_TO_PY
I'm a python script called some_script.py
I received this cmd line arg from shell VARIABLE_GIVEN_TO_SHELL_AND_PASSED_TO_PY
shell script still running after python script finished

Does this work or is Windows weirder than I thought? :)

UPDATE:

I fired up the old malware magnet and tried passing arguments from command line -> batch script -> python. I didn't use your python -x %0 %* syntax that seems to allow running python code in a batch script but simply wrote a separate .py file and called that from the .bat file.

This worked on Windows 7:

call_py.bat:

@ECHO off

set var1=%~1
echo Bat script called with cmdline arg: "%var1%"
echo Passing cmdline arg to python script
call C:\Python27\python.exe C:\Users\bob\Desktop\print_arg1.py %var1%
echo Bat again - continuing...
pause

print_arg1.py:

import sys

try:
    print "Python says hi and received this cmdline arg: " + sys.argv[1]
except IndexError:
    print "Python says hi but didn't receive any cmdline args :("

Output:

C:\Users\bob\Desktop>call_py.bat I_AM_A_CMDLINE_ARG
Bat script called with cmdline arg: "I_AM_A_CMDLINE_ARG"
Passing cmdline arg to python script
Python says hi and received this cmdline arg: I_AM_A_CMDLINE_ARG
Bat again - continuing...
Press any key to continue . . .
jDo
  • 3,962
  • 1
  • 11
  • 30
  • i thought of writing the command line argument to a new txt file. so any script can read .txt. but , while running in command prompt, bat file is not able to create .txt in C drive. but it creates in D drive smoothly.. – Running Forward Mar 08 '16 at 16:07
  • Writing to a file is perfectly fine if you can ensure that the file isn't being written and read from several scripts simultaneously. This link seems to show how to pass arguments from batch to python - in a for loop in this case: http://www.dostips.com/forum/viewtopic.php?f=3&t=3431 Maybe it's just a syntax thing and you need to prefix `var1` with one or two percent signs. – jDo Mar 08 '16 at 16:18