Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Current directory in an os.walk

Tags:

python

I need to get the current directory in an os.walk process. It works when there is just one subdirectory level but fails when there's more. Please advise...

[CODE]

# AFFECTS everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could affect all your disk files.

import os, glob, arcpy, csv, sys, shutil, datetime
top = r'L:\Raster_Data\Topographic_Maps'
RootOutput = r'L:\Raster_Data\Topographic_Maps'
#FileList = csv.reader(open('FileList.csv'))
SearchString=['Temp_Pol', 'Spatial_Ex']

filecount=0
successcount=0
errorcount=0

print "Working in: "+os.getcwd()

list =[]
f = open(RootOutput+'\\Success_LOG.txt', 'a')
f.write("Log of files Succesfully processed. RESULT of process run @:"+str(datetime.datetime.now())+"\n")
f.close()

#for File in FileList:
for root, dirs, files in os.walk(top, topdown=False):
  #for directory in dirs:
    for file in files:
      #currentPath=os.path.join(root,directory)
      currentPath=os.path.abspath(file)
      os.chdir(currentPath)
      #arcpy.env.workspace = currentPath
      #print os.getcwd()
      lstFCs = glob.glob('*'+SearchString[0]+'*.shp')
      #print lstFCs
      OutPutDir=os.path.abspath(currentPath)
      for fc in lstFCs:
          filecount=filecount+1
          list.append(OutPutDir+"\\"+fc)       

      lstFCs = glob.glob('*'+SearchString[1]+'*.shp')
      #print lstFCs
      for fc in lstFCs:
          OutPutDir=RootOutput+"\\"+directory
          filecount=filecount+1
          list.append(OutPutDir+"\\"+fc)

print 'Merging: ' + str(list)
#arcpy.Merge_management(list, RootOutput+"\\Full_Extent.shp")
print 'Created: '+RootOutput+"\\Full_Extent.shp"
f = open(RootOutput+'\\Success_LOG.txt', 'a')
f.write(str(list)+"\n\n Merged to: "+RootOutput+"\\Full_Extent.shp")
f.close()

so the list should be appended with the fc and the full path to it but just gets the root path and the final part of the path -not the directories in between.

Thanks for your advise,

[Error Messages]

Working in: L:\Raster_Data\Topographic_Maps Merging: ['L:\Raster_Data\Topographic_Maps\100K\2010_100K\Map_Sheets_BestResolution\qld_north\SE55\ecw\prj_Temp_Polygon_Extent_0.shp', 'L:\Raster_Data\Topographic_Maps\100K\2010_100K\Map_Sheets_BestResolution\qld_north\SE55\ecw\Temp_Polygon_Extent_0.shp', 'L:\Raster_Data\Topographic_Maps\ecw\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SC54\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SC55\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SD54\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SD55\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SE54\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\100K\2010_100K\Map_Sheets_BestResolution\qld_north\SE55\prj_Temp_Polygon_Extent_0.shp', 'L:\Raster_Data\Topographic_Maps\100K\2010_100K\Map_Sheets_BestResolution\qld_north\SE55\Temp_Polygon_Extent_0.shp', 'L:\Raster_Data\Topographic_Maps\SE55\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SF54\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SF55\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SF56\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SG55\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SG56\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\SH56\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\Tablelands_100K\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\200DPI\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\1M\prj_Temp_Polygon_Extent_0.shp', 'L:\Raster_Data\Topographic_Maps\1M\Temp_Polygon_Extent_0.shp', 'L:\Raster_Data\Topographic_Maps\250K\prj_Temp_Polygon_Extent_1.shp', 'L:\Raster_Data\Topographic_Maps\250K\Temp_Polygon_Extent_1.shp', 'L:\Raster_Data\Topographic_Maps\250K\Spatial_Extent.shp', 'L:\Raster_Data\Topographic_Maps\5M\prj_Temp_Polygon_Extent_2.shp', 'L:\Raster_Data\Topographic_Maps\5M\Temp_Polygon_Extent_2.shp', 'L:\Raster_Data\Topographic_Maps\5M\Spatial_Extent.shp'] Traceback (most recent call last): File "L:\Raster_Data\Topographic_Maps\CreateFileList.py", line 64, in arcpy.Merge_management(list, RootOutput+"\Full_Extent.shp")
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 3124, in Merge raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Datasets: Dataset L:\Raster_Data\Topographic_Maps\100K\2010_100K\Map_Sheets_BestResolution\qld_north\SE55\ecw\prj_Temp_Polygon_Extent_0.shp;L:\Raster_Data\Topographic_Maps\100K\2010_100K\Map_Sheets_BestResolution\qld_north\SE55\ecw\Temp_Polygon_Extent_0.shp;L:\Raster_Data\Topographic_Maps\ecw\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SC54\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SC55\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SD54\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SD55\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SE54\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\100K\2010_100K\Map_Sheets_BestResolution\qld_north\SE55\prj_Temp_Polygon_Extent_0.shp;L:\Raster_Data\Topographic_Maps\100K\2010_100K\Map_Sheets_BestResolution\qld_north\SE55\Temp_Polygon_Extent_0.shp;L:\Raster_Data\Topographic_Maps\SE55\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SF54\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SF55\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SF56\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SG55\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SG56\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\SH56\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\Tablelands_100K\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\200DPI\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\1M\prj_Temp_Polygon_Extent_0.shp;L:\Raster_Data\Topographic_Maps\1M\Temp_Polygon_Extent_0.shp;L:\Raster_Data\Topographic_Maps\250K\prj_Temp_Polygon_Extent_1.shp;L:\Raster_Data\Topographic_Maps\250K\Temp_Polygon_Extent_1.shp;L:\Raster_Data\Topographic_Maps\250K\Spatial_Extent.shp;L:\Raster_Data\Topographic_Maps\5M\prj_Temp_Polygon_Extent_2.shp;L:\Raster_Data\Topographic_Maps\5M\Temp_Polygon_Extent_2.shp;L:\Raster_Data\Topographic_Maps\5M\Spatial_Extent.shp does not exist or is not supported Failed to execute (Merge).

Working in: L:\Raster_Data\Topographic_Maps Traceback (most recent call last): File "L:\Raster_Data\Topographic_Maps\CreateFileList.py", line 28, in os.chdir(currentPath) WindowsError: [Error 2] The system cannot find the file specified: 'L:\Raster_Data\Topographic_Maps\ecw\SC54' directory 'SC54' dirs ['SC54', 'SC55', 'SD54', 'SD55', 'SE54', 'SE55'] os.path.abspath(dirs[0]) 'L:\Raster_Data\Topographic_Maps\ecw\SC54' os.getcwd() 'L:\Raster_Data\Topographic_Maps\ecw' Working in: L:\Raster_Data\Topographic_Maps Traceback (most recent call last): File "L:\Raster_Data\Topographic_Maps\CreateFileList.py", line 28, in os.chdir(currentPath) WindowsError: [Error 2] The system cannot find the file specified: 'L:\Raster_Data\Topographic_Maps\7178cp_dd.ers' file '7178cp_dd.ers' os.path.abspath os.path.abspath(file) 'L:\Raster_Data\Topographic_Maps\7178cp_dd.ers'

like image 528
GeorgeC Avatar asked Aug 26 '11 07:08

GeorgeC


People also ask

How do I walk the current directory in Python?

Get the current working directory: os. os. getcwd() returns the absolute path of the current working directory where Python is running as a string str . getcwd stands for "get current working directory", and the Unix command pwd stands for "print working directory".

How do I search for a current directory in Python?

Python can search for file names in a specified path of the OS. This can be done using the module os with the walk() functions. This will take a specific path as input and generate a 3-tuple involving dirpath, dirnames, and filenames.

Does os walk Change directory?

walk() never changes the current directory, and assumes that its caller doesn't either.

What does Python os walk return?

OS.walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).


2 Answers

Thanks all, I used the input from the forum to complete the script. It's below for anyone who wants it. best,

# AFFECTS everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could affect all your disk files.

import os, arcpy, sys, datetime
top = os.getcwd()
RootOutput = top
FileTypes=['shp']
SearchStrings=['Temp_Pol', 'Spatial_Ex']

filecount=0
#successcount=0
#errorcount=0

print "Working in: "+os.getcwd()

list =[]
f = open(RootOutput+'\\Success_LOG.txt', 'a')
f.write("Log of files Succesfully processed. RESULT of process run @:"+str(datetime.datetime.now())+"\n")
f.close()

for root, dirs, files in os.walk(top, topdown=False):
    for fl in files:
      currentFile=os.path.join(root, fl)
      for FileType in FileTypes:
          status= str.endswith(currentFile,FileType)
          if str(status) == 'True':
              for SearchString in SearchStrings:
                  if str(SearchString in currentFile) == 'True':
                    #print str(currentFile)+str(status)       
                    filecount=filecount+1
                    list.append(currentFile)

print 'Merging: ' + str(list)

#Replace with any function you want to carry out on the generated list of files. #arcpy.Merge_management(list, RootOutput+"\Full_Extent.shp")

print 'Created: '+RootOutput+"\\Full_Extent.shp"
f = open(RootOutput+'\\Success_LOG.txt', 'a')
f.write(str(list)+"\n\n Merged to: "+RootOutput+"\\Full_Extent.shp")
f.close()
like image 155
GeorgeC Avatar answered Nov 02 '22 09:11

GeorgeC


You should use

os.path.join(root, file) 

instead of simply using file like suggested in the os.walk doc examples os.walk

Btw, be careful with the reserved keywords. file is a built-in function and list too

>>> a = list()
>>> a
[]
>>> list = []
>>> b = list()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
like image 30
FlaPer87 Avatar answered Nov 02 '22 08:11

FlaPer87