Skip to content

Instantly share code, notes, and snippets.

@jelc53
Created May 24, 2023 22:12
Show Gist options
  • Save jelc53/b58b8bdf64436a448372be2ed89e429d to your computer and use it in GitHub Desktop.
Save jelc53/b58b8bdf64436a448372be2ed89e429d to your computer and use it in GitHub Desktop.
helper script to programmatically change file path text input file
import json
import os
from glob import glob
from collections import defaultdict
from argparse import ArgumentParser
import pandas as pd
TIME_LIMIT = 600
def process_arguments():
parser = ArgumentParser()
parser.add_argument("in_file", type=str, default='file_list.txt')
parser.add_argument("--change_str", type=str)
parser.add_argument("--change_idx", type=int)
args = parser.parse_args()
return args.in_file, args.change_str, args.change_idx
def main():
in_file, change_str, change_idx = process_arguments()
data_path = '/home/ubuntu/deepsolar/data'
# load file names txt
with open(os.path.join(data_path, in_file), 'r') as f:
lines = f.readlines()
# path surgery
new_paths = []
for path in lines:
# print(path)
path_dirs = path.split('/')
path_dirs[change_idx] = change_str
new_paths.append('/'.join(path_dirs))
# write to file
out_file = in_file.split('.')[0] + '_update.txt'
with open(os.path.join(data_path, out_file), 'w') as f:
for new_path in new_paths:
f.write(new_path)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment