简体   繁体   中英

modify value of variable in python program from batch file

I have a python file (example.py) to run, which contains 3 variables. I have three .txt files (var1.txt, var2.txt, var3.txt) which contains the values of each of these 3 variables.

I want to write a batch file (bfile.bat) that retrieves the three values from the text files, pass these values to the variables in the python file, and then run.

Any help would be appreciated!

-added-

Thank you. the thing is, there is no particular reason for the three files.

So a coworker of mine wrote this program in python (which I am new too), I'll say this program's name is "program.py", and she also made a file to show how it works, by setting the values of variables used in the program.py; which is "example.py". this example.py containes the values of the variables which are used in the program.py. But I want to use many values for these variables, so I wanted to make a batch file for this - which apparently is not a good idea.

So the example.py looks something like this:

SOUND = 'sound.wav'
TIMESTEP = 50
END = 500

program(SOUND, TIMESTEP, END)

and I want to change the values of SOUND, TIMESTEP, END. please help me!

Thank you!

You should consider using input parameters in your python file, making use of sys.argv . Then use bash output pipelining to pipe your information to your python script. However, I do not recommend modifying the python file from bash, by writing to it.

[Also, why is it not possible to read the files with python?]

//EDit: Regarding the new information.

So when you already got a python script, this is the best thing for you to happen, as you now have multiple ways of dealing with this.

First of all, a python script can import another python script, or just parts of it. So what you can do is import <function> from program or import program and then use it. Now you can write your own python script, using her function! You can simple create a list of your parameters and values. For example a list of tuples:

import program

# of course you can also generate this list, depeding on which couples of parameters
# you want to run :)
# this was hardcoding it for simplicity!
myparams = [('sound1.wav', 30, 50), ('sound2.wav', 20, 100), ...]

for (p1, p2, p3) in myparams:
    program.function(p1, p2, p3)

This will use the function() out of your program.py . You could also import the program and write your own script using it with sys.argv so you could run it from bash using command line parameters python myprogram.py p1 p2 p3 . However, looking at the question, you seem to be working with Windows and writing a simple python script is probably better/easier/more convenient. But that depends on your experience on that matter :)

Cheers!

It sounds like passing arguments would be an easier way to accomplish what you want. So instead of using files to hold the variables, try:

import sys

numArgs = len(sys.argv)
# You want 3 variables, and the 0th index will be '[file].py'.
if (numArgs >= 4):
    SOUND = sys.argv[1]
    TIMESTEP = sys.argv[2]
    END = sys.argv[3]
else:
    print 'Not enough arguments.'

Then you can simply run:

python [file].py arg1 arg2 arg3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM