SeismicTransformerData / MDOF /knet-250 /PythonScripts /Update_BlgAttributes_txt.py
JasonXF's picture
Add files using upload-large-folder tool
787163a verified
Raw
History Blame Contribute Delete
4.68 kB
"""
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开始。
"""
# 打开 case.txt 文件,并读取需要更新的单行数据
with open('cases.txt', 'r') as cases_file:
# 读取所有行
cases_lines = cases_file.readlines()
# 选择第二行(第一个数据行)
update_data = cases_lines[case_No].strip().split(', ')
# 读取 A.txt 文件的内容
with open('BlgAttributes.txt', 'r') as a_file:
a_lines = a_file.readlines()
# A.txt 的第二行是标题行
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')
# 更新 A.txt 中所有行的对应列
# 从第三行开始更新,因为第一行是计数,第二行是标题
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] # 更新 Stories
a_line_list[height_index] = update_data[2] # 更新 height
a_line_list[im_index] = update_data[3] # 更新 IM
a_line_list[site_cd_index] = update_data[4] # 更新 Site_CD
# 重新连接行,使用制表符作为分隔符
a_lines[i] = '\t'.join(a_line_list) + '\n'
# 将更新后的内容写入 A.txt 文件
with open('BlgAttributes.txt', 'w') as a_file:
a_file.writelines(a_lines)
# print(f"updated BlgAttributes.txt with:\n"
# f"| Stories: {update_data[1]} "
# f"| height: {update_data[2]} "
# f"| IM: {update_data[3]} "
# f"| Site_CD: {update_data[4]}")
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() # 相对路径,本python文件所在的目录
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')
# 设置源文件夹(A)和目标文件夹(B)的路径
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)
# print(f'File BlgAttributes.txt has been copy to {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()