通过批处理文件调用我自己的 python 脚本
Calling my own python script via a 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
我写了 "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])
当我在我的 IDE 中 运行 时,"replace_first_line.py" 工作正常,将 "argv[1]" 替换为我将传递到批处理文件的相同参数。当我用那个参数调用批处理文件时,它不起作用(我先把它改回"argv[1]")
其余的脚本都可以工作,那有什么用呢?不可否认,我的脚本是在 Python 3 环境中编写的,但我认为其中没有任何东西不在 Python 2.
中
Sending arguments from Batch file to Python script
看看这个,它可能有帮助。批处理文件和 python 脚本使用参数的方式不同,因此这可能与您传递参数的方式有关。
底线:您正在将文件名插入到文件名中。
在你的 python 文件中,你想做
replace_first_line(argv[1],argv[1].replace(".csv","namefixed.csv")
否则,如果您将 20150915 传递给您的批处理文件,最终传递给 replace_first_line 的是 F:\electoral_map\F:\electoral_map150915\candidates\candidates_Conservative.csv\candidates\candidates_Conservative.csv
(注意两个 f:\ 等)
我在 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
我写了 "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])
当我在我的 IDE 中 运行 时,"replace_first_line.py" 工作正常,将 "argv[1]" 替换为我将传递到批处理文件的相同参数。当我用那个参数调用批处理文件时,它不起作用(我先把它改回"argv[1]")
其余的脚本都可以工作,那有什么用呢?不可否认,我的脚本是在 Python 3 环境中编写的,但我认为其中没有任何东西不在 Python 2.
中Sending arguments from Batch file to Python script
看看这个,它可能有帮助。批处理文件和 python 脚本使用参数的方式不同,因此这可能与您传递参数的方式有关。
底线:您正在将文件名插入到文件名中。 在你的 python 文件中,你想做
replace_first_line(argv[1],argv[1].replace(".csv","namefixed.csv")
否则,如果您将 20150915 传递给您的批处理文件,最终传递给 replace_first_line 的是 F:\electoral_map\F:\electoral_map150915\candidates\candidates_Conservative.csv\candidates\candidates_Conservative.csv
(注意两个 f:\ 等)