Check does file exist in folder

1215
4
Jump to solution
04-20-2018 08:12 AM
JohnMcConalogue
New Contributor III

I am writing a python script which deletes a file if it is present in a particular folder.

I want to be able to check if the file exists first, and if it exists I want to delete the file but if it doesn't exist I want to do nothing.

Anyone any ideas on this.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KevinDunlop
Occasional Contributor III
0 Kudos
4 Replies
KevinDunlop
Occasional Contributor III
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Use arcpy...

arcpy.Exists.... http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/exists.htm

then there is the whole arcpy.ListFeatureClasses, ListRasters, Listwhatever if you need things specific to geodatabases etc.

ie  http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/listfeatureclasses.htm

JoshuaBixby
MVP Esteemed Contributor

Just use Delete—Help | ArcGIS Desktop first thing.  If the data set or file/folder exists, Delete takes care of business.  If it doesn't exist, Delete still succeeds so there is no error to trap.  The Result—Help | ArcGIS Desktop  from Delete will include a warning if the file doesn't exist, but the operation still succeeds. 

JohnMcConalogue
New Contributor III

Thanks for the reply guys

A modification of Kevin's seemed to do the trick.

0 Kudos