0

I wrote a script in Python and then found another one on github that is very useful. I would like to automate a larger task containing both of these scripts. I have a batch file as follows:

for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
chcp 1252>nul
cd C:\python_projects\json_to_csv
gen_outline.py --collection objects F:\electoral_map\%1\candidates\candidates_Bloc_Québécois.json
json2csv.py F:\electoral_map\%1\candidates\candidates_Bloc_Québécois.json F:\electoral_map\%1\candidates\candidates_Bloc_Québécois.outline.json
gen_outline.py --collection objects F:\electoral_map\%1\candidates\candidates_Christian_Heritage.json
json2csv.py F:\electoral_map\%1\candidates\candidates_Christian_Heritage.json F:\electoral_map\%1\candidates\candidates_Christian_Heritage.outline.json
gen_outline.py --collection objects F:\electoral_map\%1\candidates\candidates_Conservative.json
json2csv.py F:\electoral_map\%1\candidates\candidates_Conservative.json F:\electoral_map\%1\candidates\candidates_Conservative.outline.json
gen_outline.py --collection objects F:\electoral_map\%1\candidates\candidates_Forces_et_Démocratie.json
json2csv.py F:\electoral_map\%1\candidates\candidates_Forces_et_Démocratie.json F:\electoral_map\%1\candidates\candidates_Forces_et_Démocratie.outline.json
gen_outline.py --collection objects F:\electoral_map\%1\candidates\candidates_Green_Party.json
json2csv.py F:\electoral_map\%1\candidates\candidates_Green_Party.json F:\electoral_map\%1\candidates\candidates_Green_Party.outline.json
gen_outline.py --collection objects F:\electoral_map\%1\candidates\candidates_Liberal.json
json2csv.py F:\electoral_map\%1\candidates\candidates_Liberal.json F:\electoral_map\%1\candidates\candidates_Liberal.outline.json
gen_outline.py --collection objects F:\electoral_map\%1\candidates\candidates_Libertarian.json
json2csv.py F:\electoral_map\%1\candidates\candidates_Libertarian.json F:\electoral_map\%1\candidates\candidates_Libertarian.outline.json
gen_outline.py --collection objects F:\electoral_map\%1\candidates\candidates_NDP.json
json2csv.py F:\electoral_map\%1\candidates\candidates_NDP.json F:\electoral_map\%1\candidates\candidates_NDP.outline.json
replace_first_line.py F:\electoral_map\%1\candidates\candidates_Conservative.csv F:\electoral_map\%1\candidates\candidates_Conservative_namefixed.csv
chcp %cp%>nul
pause

I wrote "replace_first_line.py" and the other two scripts ("gen_outline.py" and "json2csv.py") were taken from github.

This is replace_first_line.py:

from sys import argv

def replace_first_line(fin_path, fout_path):

    with open(fin_path) as fin:
        lines = fin.readlines()
        lines[0] = lines[0].replace('district_name,elected_office,election_name,email,extra_facebook,extra_instagram,extra_linkedin,extra_twitter,extra_youtube,first_name,gender,incumbent,last_name,name,offices_0_tel,offices_0_type,party_name,personal_url,photo_url,related_boundary_url,related_election_url,source_url,url',
                                    'district_name,elected_office,election_name,email,extra_facebook,extra_instagram,extra_linkedin,extra_twitter,extra_youtube,first_name,gender,incumbent,last_name,name_,offices_0_tel,offices_0_type,party_name,personal_url,photo_url,related_boundary_url,related_election_url,source_url,url')

    with open(fout_path, 'w') as fout:
        for line in lines:
            fout.write(line)

replace_first_line(argv[1], argv[2])

The "replace_first_line.py" works fine when I run it in my IDE, replacing "argv[1]" for the same parameter that I would pass into the batch file. When I call the batch file with that parameter, it does not work (I changed it back to "argv[1]" first)

The rest of the scripts work, so what gives? Admittedly, my script was written in a Python 3 environment, but I don't think that there is anything in there that isn't also in Python 2.

3
  • In your batch file you're passing in F:\electoral_map\%1\candidates\candidates_Conservative.csv F:\electoral_map\%1\candidates\candidates_Conservative_namefixed.csv; in your function, you're sticking that into {0} ... that's going to result in replace_first_line getting passed F:\electoral_map\F:\electoral_map\{WHATEVER %1 gets set to}\candidates\candidates_Conservative.csv\candidates\candidates_Conservative.csv ... I don't think that's what you want... is that a typo in your question or your problem? Commented Sep 15, 2015 at 22:23
  • It's not a mistake, what actually gets passed as %1 is 20150915 (representing a date in format YYYYMMDD, this date is the name of the folder that I am working in that day). So what I actually call is my_bat_file.bat 20150915 and it should do the rest on its own Commented Sep 15, 2015 at 22:39
  • To clarify, I think you're confusing what argv is going to be... there's the argument you pass to the batch file (%1)... but sys.argv[] inside your python program is the first argument passed to it (which is going to be F:\electoral_map\%1\candidates\candidates_Conservative.csv) Commented Sep 15, 2015 at 22:56

2 Answers 2

1

Bottom line: you're inserting the filename into the filename. In your python file, you want to do

replace_first_line(argv[1],argv[1].replace(".csv","namefixed.csv")

As otherwise, what gets ultimately passed to replace_first_line if you pass in 20150915 to your batch file is F:\electoral_map\F:\electoral_map\20150915\candidates\candidates_Conservative.csv\candidates\candidates_Conservative.csv (note the two f:\ etc.)

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

2 Comments

I see what you mean and I changed the python script and batch file in the original post, but it still doesn't seem to be working.
I'm getting so frustrated with this, when I copy+paste the exact arguments out of the batch file and drop them into the python script (surrrounding it with r"" and chaning %1 to 20150915), it works fine. But when I call the batch file, it doesn't work. LOL NEVERMIND IM A MORON! I was actually editing a different version of the script that I had erroneously saved in a different folder. Boy is my face red. Thanks for your help.
1

Sending arguments from Batch file to Python script

check this out, it may help. batch files and python scripts use arguments differently and so it may have something to do with how you are passing the argument.

Comments

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.