Skip to content

Instantly share code, notes, and snippets.

@hovedguy
Created August 26, 2021 07:53
Show Gist options
  • Select an option

  • Save hovedguy/78f5dd62c574840d5964b673448d5c20 to your computer and use it in GitHub Desktop.

Select an option

Save hovedguy/78f5dd62c574840d5964b673448d5c20 to your computer and use it in GitHub Desktop.
import os
import re
import shutil
from tqdm import tqdm
mainDir = r"C:\SUVOPAM_local\Dolwin5\02_Aba\boatLanding\specialCases\gl1\_withHydroMassFact"
omittInp = None # None if nothing to remove
newText = '396000.0, \n'
subDirDumpNewInps = '_modifiedInps'
os.chdir(mainDir)
files = []
for item in os.listdir(os.getcwd()):
if item.endswith(".inp"):
files.append(item)
if omittInp != None:
files.remove('_masterOut.inp')
def lineLocatorFromKeyword(lines, kw, kwStartBool=False):
found_index = []
for i, line in enumerate(lines):
if not kwStartBool:
if bool(re.search(kw, line, re.MULTILINE)):
found_index.append(i)
else:
if bool(re.match(kw, line, re.MULTILINE)):
found_index.append(i)
return found_index
def createEmptySubDir(mainDir, subDirName):
'''
:param mainDir:
:param subDirName: folder name which is to create
:return:
'''
picFullDir = os.path.join(mainDir, subDirName)
if os.path.exists(picFullDir) and os.path.isdir(picFullDir):
shutil.rmtree(picFullDir)
os.makedirs(picFullDir)
return picFullDir
newDir = createEmptySubDir(mainDir, subDirDumpNewInps)
newFileFullPaths = [os.path.join(newDir, f"{x.split('.')[0]}_MODIFIED.inp") for x in files]
for orgFile, newFileFullPath in tqdm(list(zip(files, newFileFullPaths)), desc='Progress', bar_format='{l_bar}{bar:60}{r_bar}{bar:-10b}'):
with open(orgFile, 'r') as f:
lines = f.readlines()
i = lineLocatorFromKeyword(lines, '\*Mass,', kwStartBool=True)[0]
lines[i + 1] = newText
with open(newFileFullPath, 'w') as fw:
fw.writelines(lines)
print(f"inp files are written at: {newDir}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment