| """
|
| Author: Jason Jiang
|
| Date: 2024.01.27
|
|
|
| Move BlgAttributes.txt to each folder's inputs floder to renew the training data.
|
| """
|
|
|
| import os
|
| import shutil
|
| import re
|
| from time import sleep
|
|
|
| def update_blgattributes(case_No:int):
|
| """更新 blgattributes.txt 文件。
|
|
|
| 首先从外部的 cases.txt 文件中读取需要更新的情况,然后根据情况更新 blgattributes.txt 文件。
|
|
|
| Args:
|
| case_No (int): 从1开始。
|
|
|
| """
|
|
|
| with open('cases.txt', 'r') as cases_file:
|
|
|
| cases_lines = cases_file.readlines()
|
|
|
| update_data = cases_lines[case_No].strip().split(', ')
|
|
|
|
|
| with open('BlgAttributes.txt', 'r') as a_file:
|
| a_lines = a_file.readlines()
|
|
|
|
|
| headers = a_lines[1].strip().split('\t')
|
|
|
| stories_index = headers.index('Stories')
|
| height_index = headers.index('height')
|
| im_index = headers.index('IM')
|
| site_cd_index = headers.index('Site_CD')
|
|
|
|
|
|
|
| for i in range(2, len(a_lines)):
|
|
|
| a_line_list = a_lines[i].strip().split('\t')
|
|
|
| if len(a_line_list) == 1:
|
| continue
|
|
|
| a_line_list[stories_index] = update_data[1]
|
| a_line_list[height_index] = update_data[2]
|
| a_line_list[im_index] = update_data[3]
|
| a_line_list[site_cd_index] = update_data[4]
|
|
|
| a_lines[i] = '\t'.join(a_line_list) + '\n'
|
|
|
|
|
| with open('BlgAttributes.txt', 'w') as a_file:
|
| a_file.writelines(a_lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| cases_file.close()
|
| a_file.close()
|
|
|
| results = {"Stories": update_data[1],
|
| "height": update_data[2],
|
| "IM": update_data[3],
|
| "Site_CD": update_data[4]}
|
|
|
| return results
|
|
|
| def update_blgattributes_txt():
|
| """
|
| 将 BlgAttributes.txt 文件移动到每个文件夹的 inputs 文件夹中。
|
| """
|
| target_directory = os.getcwd()
|
| pattern = re.compile(r'\d')
|
|
|
| for folder_name in os.listdir(target_directory):
|
|
|
| folder_path = os.path.join(target_directory, folder_name)
|
|
|
| if os.path.isdir(folder_path) and pattern.search(folder_name):
|
|
|
| target_path = os.path.join(folder_path, 'inputs')
|
|
|
|
|
| source_file_path = os.path.join(target_directory, 'BlgAttributes.txt')
|
| target_file_path = os.path.join(target_path, 'BlgAttributes.txt')
|
|
|
|
|
| if os.path.isfile(source_file_path):
|
| if os.path.isfile(target_file_path):
|
|
|
| for i in range(3):
|
| try:
|
| os.remove(target_file_path)
|
| break
|
| except PermissionError:
|
| print(f"PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '{target_file_path}'")
|
| sleep(1)
|
| pass
|
|
|
| shutil.copy(source_file_path, target_file_path)
|
|
|
| else:
|
| print(f'The source file BlgAttributes.txt was not found in {source_file_path}.')
|
|
|
| print("All BlgAttributes.txt files have been updated.")
|
|
|
| def main():
|
| update_blgattributes(case_No=1)
|
| update_blgattributes_txt()
|
|
|
| if __name__ == "__main__":
|
| main() |