wfgeo
wfgeo

Reputation: 3098

Calling my own python script via a batch file

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.

Upvotes: 0

Views: 108

Answers (2)

Foon
Foon

Reputation: 6458

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.)

Upvotes: 1

R Nar
R Nar

Reputation: 5515

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.

Upvotes: 1

Related Questions