Python:os.walk中的当前目录

Geo*_*geC 5 python

我需要在os.walk进程中获取当前目录.它只在一个子目录级别有效,但在有更多时失败.请指教...

[码]

# 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()
Run Code Online (Sandbox Code Playgroud)

所以列表应该附加fc和它的完整路径,但只是获取根路径和路径的最后部分 - 而不是之间的目录.

感谢您的意见,

[错误讯息]

工作位置:L:\ Raster_Data\Topographic_Maps合并:['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_Exte nt.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(最近一次调用最后一次):文件"L:\ Raste r_Data\Topographic_Maps\CreateFileList.py",第64行,在arcpy.Merge_management(list,RootOutput +"\ Full_Extent.shp")
文件"C:\ Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py",第3124行,在Merge中引发e执行错误:执行失败.参数无效.错误000732:输入数据集:数据集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\Spatia l_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不存在或不受支持执行失败(合并).

使用方法:L:\ Raster_Data\Topographic_Maps Traceback(最近调用最后一次):文件"L:\ Raster_Data\Topographic_Maps\CreateFileList.py",第28行,在os.chdir(currentPath)中WindowsError:[错误2]系统不能找到指定的文件:'L:\ Raster_Data\Topographic_Maps\ecw\SC54'目录'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'工作于:L:\ Raster_Data\Topographic_Maps Traceback(最近一次调用最后一次) ):文件"L:\ Raster_Data\Topographic_Maps\CreateFileList.py",第28行,在os.chdir(currentPath)中WindowsError:[错误2]系统找不到指定的文件:'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'

Geo*_*geC 7

谢谢大家,我使用了论坛的输入来完成脚本。下面是给任何想要的人的。最好的,

# 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)
Run Code Online (Sandbox Code Playgroud)

#替换为您想要在生成的文件列表上执行的任何函数。#arcpy.Merge_management(列表, 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()
Run Code Online (Sandbox Code Playgroud)