Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Python script: each time write output w/o overwrite

Status
Not open for further replies.

EvgenAbaqus

Mechanical
Jan 27, 2015
17
Hi there!
I have a script which can write *.csv file with name lets say 1.csv
I'm going to preform parametric study, so ODB name will be same.
If do so script will just overwrite my 1.csv file and that's it.
How to change a script so it will check existed name (number) and add +1 instead overwriting
so it will write files like this:

1.csv
2.csv
...
n+1.csv

jobName = '''DOE'''
stepName = '''Step-1'''
outputSetName = '''SET-1'''

from odbAccess import*
from abaqusConstants import*
import string
import numpy as np
import os
import datetime

odb = openOdb(path = jobName+'.odb')

outfile = open('C:/DOE/1' + '.csv', 'w')

for fm in range(0, len(odb.steps[stepName].frames)):
timeFrame = odb.steps[stepName].frames[fm]
readNode = odb.rootAssembly.nodeSets[outputSetName.upper()]
Disp = timeFrame.fieldOutputs['U']
ReForce = timeFrame.fieldOutputs['RF']
readNodeDisp = Disp.getSubset(region=readNode)
readNodeDispValues = readNodeDisp.values
readNodeRF = ReForce.getSubset(region=readNode)
readNodeRFValues = readNodeRF.values
Displacement = np.zeros(len(odb.steps[stepName].frames))
ReactionForce = np.zeros(len(odb.steps[stepName].frames))
Displacement[fm] = readNodeDispValues[0].data[1] # 0-X Direction; 1-Y Direction; 2-Z Direction
ReactionForce[fm] = readNodeRFValues[0].data[1]
outfile.write(str(Displacement[fm]) + ',' + str(ReactionForce[fm]) + ',' + '\n')

outfile.close()

odb.close()


Attached ODB file:
 
 https://files.engineering.com/getfile.aspx?folder=fe457208-0895-4cc3-80f5-ddf4e4745eae&file=DOE_odb.rar
Replies continue below

Recommended for you

Just check first which filenames already exist using os.path.isfile(filename):

import os
n=1
while True:
fname=str(n)+'.csv'
if os.path.isfile(fname):
#file exists, append counter
n=n+1
else:
#file not found, exit loop
break

print(fname) #here you have the first free filename
outfile = open('C:/DOE/'+fname, 'w')
#rest of the code here

 
Code:
jobName = '''DOE'''
stepName = '''Step-1'''
outputSetName = '''SET-1'''

from odbAccess import*
from abaqusConstants import*
import string
import numpy as np
import os
import datetime

odb = openOdb(path = jobName+'.odb')


n=1

while True:
    fname=str(n)+'.csv'
    if os.path.isfile(fname):
      n=n+1
    else:
      break

print(fname) #here you have the first free filename

outfile = open('C:/DOE/'+fname, 'w')

#rest of the code here

for fm in range(0, len(odb.steps[stepName].frames)):
  timeFrame = odb.steps[stepName].frames[fm]
  readNode = odb.rootAssembly.nodeSets[outputSetName.upper()]
  Disp = timeFrame.fieldOutputs['U']
  ReForce = timeFrame.fieldOutputs['RF']
  readNodeDisp = Disp.getSubset(region=readNode)
  readNodeDispValues = readNodeDisp.values
  readNodeRF = ReForce.getSubset(region=readNode)
  readNodeRFValues = readNodeRF.values
  Displacement = np.zeros(len(odb.steps[stepName].frames))
  ReactionForce = np.zeros(len(odb.steps[stepName].frames))
  Displacement[fm] = readNodeDispValues[0].data[1] # 0-X Direction; 1-Y Direction; 2-Z Direction
  ReactionForce[fm] = readNodeRFValues[0].data[1]
  outfile.write(str(Displacement[fm]) + ',' + str(ReactionForce[fm]) + ',' + '\n')

outfile.close()

odb.close()


but even file 1.csv exists it will not create file 2.csv(( -> it will overwrite 1.csv again
 
Your work directory is probably different than the directory you want to save the files to. Try with

if os.path.isfile('C:/DOE/'+fname):
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor