0

I'm having a small error trying to delete some files using the OS module in python and I'm a little confused. I'm trying to run the following code:

import arcpy, os

# Set environment settings
arcpy.env.workspace = r"M:\GIS\Oscar\2021\Grid Script\2022\Split Data"

for file in os.listdir(r"M:\GIS\Oscar\2021\Grid Script\2022\Split Data"):

    if file.endswith('.lyr') == True:
        # Delete previously created layer
        print 'Removing previous .lyr files in: '+str(arcpy.env.workspace)
        os.remove(file)
    else:
        print 'No .lyr files were found in: '+str(arcpy.env.workspace)

But running this code throws me this error:

  File "C:/Users/fitzpatricko_adm/AppData/Roaming/JetBrains/PyCharmCE2021.2/scratches/scratch_4.py", line 11, in <module>
    os.remove(file)
WindowsError: [Error 2] The system cannot find the file specified: 'Layer Data.lyr'

The reason I'm confused is that the file I'm looking for seems to be found when I run this code in my python console:

for file in os.listdir(r"M:\GIS\Oscar\2021\Grid Script\2022\Split Data"):
    if file.endswith('.lyr') == True:
        print file
        
Layer Data.lyr

This is probably a silly question, but any help is much appreciated. I had thought that I might be making a rookie error with how I structured my loop's logic, or perhaps my arcpy env variable is being awkward?

For reference, this is what my loop is finding in the directory:

for file in os.listdir(r"M:\GIS\Oscar\2021\Grid Script\2022\Split Data"):
    print file
    
(BNG) 27700.prj
Proj1 2021 5.csv
Proj1 2020 5.sbx
Proj1 2020 5.dbf
Proj1 2020 5.sbn
Proj1 2020 5.shp.xml
Proj2 2021 8.csv
Proj2 2021 5.csv
Proj1 2020 8.csv
Proj2 2020 8.csv
Layer Data.lyr
Proj1 2020 5.prj
Proj2 2020 5.csv
Proj1 2020 11.csv
Proj1 2021 8.csv
Proj1 2020 5.cpg
Proj1 2020 5.shp
Proj1 2021 11.csv
Proj2 2020 11.csv
Proj1 2020 5.shx
Proj1 2020 5.csv
Proj2 2021 11.csv

Thanks for looking!

  • 1
    Quick aside, you don't need to write `if file.endswith('.lyr') == True`. Just write `if file.endswith('.lyr')` – Iguananaut Feb 01 '22 at 17:33

2 Answers2

0

You have to use the absolute path. Try this:

import arcpy, os

# Set environment settings
arcpy.env.workspace = r"M:\GIS\Oscar\2021\Grid Script\2022\Split Data"

for file in os.listdir(r"M:\GIS\Oscar\2021\Grid Script\2022\Split Data"):

    if file.endswith('.lyr') == True:
        # Delete previously created layer
        print 'Removing previous .lyr files in: '+str(arcpy.env.workspace)
        os.remove("M:\GIS\Oscar\2021\Grid Script\2022\Split Data//" + file)
    else:
        print 'No .lyr files were found in: '+str(arcpy.env.workspace)
ksohan
  • 1,165
  • 2
  • 9
  • 23
  • 1
    If doing like this, try assigning the directory name to a global variable in one place, or better yet accept it as a command-line argument – Iguananaut Feb 01 '22 at 17:37
  • All the backslashes need to be doubled or esceped, or use a raw string. You don't need to double forward slash. – Barmar Feb 01 '22 at 17:39
  • You are right. It would be better to store the directory path in a variable and then use it. I hope OP will do that – ksohan Feb 01 '22 at 17:39
  • Just changed the slash! @Barmar – ksohan Feb 01 '22 at 17:40
0

You saw for yourself that os.listdir just returns the filename without the full directory path. You can either os.path.join(<dir>, filname) or alternatively use the glob module:

>>> import glob
>>> glob.glob('/path/to/*.lyr')
Iguananaut
  • 21,810
  • 5
  • 50
  • 63