0

I have a python script "cost_and_lead_time.py" in C:\Desktop\A. This script imports 3 other scripts and a "cost_model.json" file, all of which are in folder A.

Now, I have a simulation result in say C:\Desktop\Model\Results. I have a one line batch file in this folder of "call C:\Desktop\A\cost_and_lead_time.py", but it returns an error when it tries to open the cost_model.json file. It doesn't appear to have an issue importing the 3 other scripts as those appear before the json is opened.

My question is, is there any way to keep this cost_model.json file in that directory and run the script through the batch file without copy/pasting the json file into the results folder? The only way I can think of is to hard code the full path of the file in the python script, but that isn't ideal for me. I'm looking for code to add to the batch file, not python script.

Thanks

2
  • How are you opening the the cost_model.json file? Does it assume it's in the current working directory -- if so, fix that. Commented Jul 26, 2013 at 17:17
  • show us your .BAT code, please. In particular I'd like to see how it does specify the name of the json file for the python script to load it. Commented Jul 26, 2013 at 17:17

2 Answers 2

1

'cd path' in bat file or add this code to python executed file.

import os,sys
path = os.path.abspath(os.path.dirname(__file__))
os.chdir(path) #for relative files
sys.path.insert(0,path) #for modules load

working files paths passed to script must be rewrited to with os.path.abspath while parsing arguments before os.chdir. Or not to use chdir and rewrite static paths

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

Comments

0

Ultimately there needs to be some way to tell your script what you are needing to process. There's a variety of ways for that, but it really just needs to happen. I think the most obvious thing to do in the batch file is to copy the target file in place before running your Python script.

However, a cleaner solution might be to take a command line argument into the python script (via sys.argv) which tells the script which file it needs to process.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.