通过批处理文件调用我自己的python脚本

时间:2015-09-15 22:18:40

标签: python batch-file

我在Python中编写了一个脚本,然后在github上找到另一个非常有用的脚本。我想自动执行包含这两个脚本的更大任务。我有一个批处理文件如下:

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

我写了#34; replace_first_line.py"另外两个脚本(" gen_outline.py"和" json2csv.py")来自github。

这是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])

" replace_first_line.py"当我在我的IDE中运行它时,工作正常,取代" argv [1]"对于我将传递到批处理文件的相同参数。当我用该参数调用批处理文件时,它不起作用(我将其改回" argv [1]" first)

其余的脚本都可以工作,那么是什么呢?不可否认,我的脚本是在Python 3环境中编写的,但我不认为Python 2中也有任何内容。

2 个答案:

答案 0 :(得分:1)

Sending arguments from Batch file to Python script

检查一下,这可能会有所帮助。批处理文件和python脚本使用不同的参数,因此它可能与您传递参数的方式有关。

答案 1 :(得分:1)

底线:您将文件名插入文件名。 在你的python文件中,你想要做

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

另外,如果您将20150915传递到批处理文件,最终会传递给replace_first_line的是F:\electoral_map\F:\electoral_map\20150915\candidates\candidates_Conservative.csv\candidates\candidates_Conservative.csv (注意两个f:\ etc。)