Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Pyton Script: Check If File Exists In Folder

1 REPLY 1
Reply
Message 1 of 2
isocam
128 Views, 1 Reply

Pyton Script: Check If File Exists In Folder

Can anybody help?

 

Is it possible, using a Python script, to check if a file exists in a particular folder?

 

Please see the attached picture.

 

For example:

 

Say I have a file, saved in the "Reference Parts" folder, called 12345

 

Can I use the search string 12345 to search the folder "Reference Parts" to see if the file exists, or not.

 

Return True if it exists or False if it does not.

 

Many thanks in advance!

 

Darren

1 REPLY 1
Message 2 of 2
BrianEkins
in reply to: isocam

Here's an example that does what you want.

 

def CheckForDesign():
    try:
        projName = 'Reference Parts'
        designName = '12345'
        if DesignExists(projName, designName):
            ui.messageBox(f'Design {designName} exists in {projName}')
        else:
            ui.messageBox(f'Design {designName} does NOT exist in {projName}')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def DesignExists(projectName: str, designName: str) -> bool:
    app = adsk.core.Application.get()
    
    proj: adsk.core.DataProject
    for proj in app.data.dataProjects:
        if proj.name == projectName:
            file: adsk.core.DataFile
            for file in proj.rootFolder.dataFiles:
                if file.name == designName:
                    return True

    return False
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report