Message 1 of 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to see whether a document already exists within a subfolder of a project. The code to check looks as follows:
def file_exists(parent: adsk.core.DataFolder, name: str):
files = parent.dataFiles.asArray()
for file in files:
if file.name == name:
return True
return False
It's then used in a loop as follows
def run(context):
try:
ui = None
app = adsk.core.Application.get()
ui = app.userInterface
project: adsk.core.DataProject = app.data.activeProject
for d in items:
name = d.name
folder = project.rootFolder.dataFolders.itemByName("Items")
if file_exists(folder, name):
continue
doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
# do stuff
doc.saveAs(name, folder, '', '')
doc.close(False)
Say the items were A, B, C, D, and E. The first time it runs, it will succeed for A, and A is created, but throw an exception for B. When you run it again, the function succeeds for A and B, but then fails for C, and so on and so forth. The error being produced is this:
I have tried other combinations suggested in the forum, namely by looping through the data files using an index, enumerating through them. But they all fail with an error similar to this.
How does one reliably check if a file exists in a folder of a project?
Software Engineer
https://wernerstrydom.com
https://wernerstrydom.com
Solved! Go to Solution.