0

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!

2
  • 2
    What can't you get the values from the text files with python? Commented Jun 27, 2013 at 14:26
  • I am new to python so I'm trying to figure out this problem the best way! thank you. Commented Jun 27, 2013 at 15:34

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

4 Comments

I am new to both batch file scripting and python, so if there are any better ways to do this, I am happy to listen.
@hame Well it would be best, if you would tell us more about your problem :) The best way would be to read the files with python. But why are there 3 files? Are you allowed to read it?
@hame Hey, please consider my new answer and I try to help you further :)
Thank you!! I haven't got the chance to try it out yet, but it seems comprehensible for me so far :D I am working on Linux, but I'll try to make it work !
0

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

1 Comment

Aha, that's how the sys.argv is used. thank you for the information!! :D

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.