Grasshopper

algorithmic modeling for Rhino

Hi everyone,

I have a bunch of STP files that I need to rename.Each file is a fabrication file which we need to control the names based on part names, but the files exported with different names. Each file has a block inside of it which has the name I need. For example:

File name = prd-R1132100503416-00002721.stp

Contents of file = 1 block named "PART_NAME".

and I want to make the file name = PART_NAME.stp

I have been playing with the os.rename function but that requires me to know exactly what name each file needs. 

Is there a way that i can extract the object in the file and get its name which I can then use for the rename function?

Views: 714

Replies to This Discussion

Thanks, Tom, I wound up doing pretty much exactly this. I didn't parse the file line by line, but I searched for the prefix of my naming convention and then trimmed it all down to just the name. It was pretty simple and ran nice and fast. here's my code:

import os

os.chdir(path)


if Run:
names = []
files = []
i=0
for file_name in os.listdir(path):
originalName = os.path.join(path,file_name)
files.append(originalName)
fileToOpen = open(originalName,'r')
FILE = fileToOpen.read()
index = FILE.find('N_')
partNum = FILE[index:index+10]
fileToOpen.close()
lastIndex = len(partNum)-1
#Tweaking name based on length of the string
if partNum[lastIndex] == "'":
partNum = partNum[0:9]
else:
partNum = partNum
new_name = partNum+'.stp'

NAME = os.path.join(path,new_name)
names.append(partNum)
names.append(NAME)
os.rename(originalName,NAME)
i+=1


a = names

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service