Created
September 20, 2019 12:56
-
-
Save give-you-favo/27fe6c21ee940fcf00c826b6e4249412 to your computer and use it in GitHub Desktop.
globのpathname形式で指定したファイルを、csvで定義したリストに従って置換するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import csv | |
import os | |
def read_csv_list(csv_path): | |
with open(csv_path, "r", encoding="utf-8") as f: | |
reader = csv.reader(f) | |
csv_list = [r for r in reader] | |
return csv_list | |
def get_file_list(path_name): | |
return glob.glob(path_name) | |
def replace_and_write(file, file_to_write, replace_list): | |
with open(file, mode="r", encoding="utf-8") as f: | |
data = f.read() | |
with open(file_to_write, mode="w", encoding="utf-8") as f: | |
for replace_pair in replace_list: | |
data = data.replace(replace_pair[0], replace_pair[1]) | |
f.write(data) | |
def replace_by_list(target_path_name, dist_folder, csv_path): | |
file_list = get_file_list(target_path_name) | |
replace_list = read_csv_list(csv_path) | |
if not os.path.exists(dist_folder): | |
os.mkdir(dist_folder) | |
for file in file_list: | |
filename = os.path.basename(file) | |
replace_and_write(file, dist_folder + filename, replace_list) | |
replace_by_list("target/*.txt", "dist/", "list.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment