Pyton Script: Check If File Exists In Folder

Pyton Script: Check If File Exists In Folder

isocam
Collaborator Collaborator
425 Views
1 Reply
Message 1 of 2

Pyton Script: Check If File Exists In Folder

isocam
Collaborator
Collaborator

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

0 Likes
426 Views
1 Reply
Reply (1)
Message 2 of 2

BrianEkins
Mentor
Mentor

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
0 Likes