【问题标题】:How to get Python to look at Sub-Folders?如何让 Python 查看子文件夹?
【发布时间】:2013-06-26 20:39:26
【问题描述】:

我正在尝试创建一个 python 脚本,它将查看一系列子文件夹并删除空的 shapefile。我已经成功创建了将删除一个文件夹中的空文件的脚本部分,但是“项目”文件夹中总共有 70 个文件夹。虽然我可以复制和粘贴代码 69 次,但我确信必须是一种让它查看每个子文件夹并为每个子文件夹运行代码的方法。以下是我到目前为止所拥有的。有任何想法吗?我对此很陌生,我只是简单地编辑了一个现有的代码来做到这一点。谢谢!

import os

# Set the working directory
os.chdir ("C:/Naview/Platypus/Project")

# Get the list of only files in the current directory
file = filter(os.path.isfile, os.listdir('C:/Naview/Platypus/Project'))
# For each file in directory
for shp in file:
    # Get only the files that end in ".shp"
    if shp.endswith(".shp"):
        # Get the size of the ".shp" file.
        # NOTE: The ".dbf" file can vary is size whereas
        #       the shp & shx are always the same when "empty".
        size = os.path.getsize(shp)
        print "\nChecking " + shp + "'s file size..."

        #If the file size is greater than 100 bytes, leave it alone.                  
        if size > 100:
            print "File is " + str(size) + " bytes"
            print shp + " will NOT be deleted \n"

        #If the file size is equal to 100 bytes, delete it.      
        if size == 100:
            # Convert the int output from (size) to a string.
            print "File is " + str(size) + " bytes"                    
            # Get the filename without the extention
            base = shp[:-4]
            # Remove entire shapefile
            print "Removing " + base + ".* \n"
            if os.path.exists(base + ".shp"):
               os.remove(base + ".shp")
            if os.path.exists(base + ".shx"):
                os.remove(base + ".shx")
            if os.path.exists(base + ".dbf"):
                os.remove(base + ".dbf")
            if os.path.exists(base + ".prj"):
                os.remove(base + ".prj")
            if os.path.exists(base + ".sbn"):
                os.remove(base + ".sbn")
            if os.path.exists(base + ".sbx"):
                os.remove(base + ".sbx")
            if os.path.exists(base + ".shp.xml"):
                os.remove(base + ".shp.xml")

【问题讨论】:

    标签: python directory subdirectory


    【解决方案1】:

    有几种方法可以做到这一点。我是glob的粉丝

    for shp in glob.glob('C:/Naview/Platypus/Project/**/*.shp'):
        size = os.path.getsize(shp)
        print "\nChecking " + shp + "'s file size..."
    
        #If the file size is greater than 100 bytes, leave it alone.                  
        if size > 100:
            print "File is " + str(size) + " bytes"
            print shp + " will NOT be deleted \n"
            continue
        print "Removing", shp, "files"
        for file in glob.glob(shp[:-3] + '*'):
            print " removing", file
            os.remove(file)
    

    【讨论】:

      【解决方案2】:

      学习过程式编程的时间:Defining Functions

      将您的代码放入带有路径参数的函数中,并为您的 70 个路径中的每一个调用它:

      def delete_empty_shapefiles(path):
          # Get the list of only files in the current directory
          file = filter(os.path.isfile, os.listdir(path))
          ...
      
      paths = ['C:/Naview/Platypus/Project', ...]
      for path in paths:
          delete_empty_shapefiles(path)
      

      创建执行os.path.exists()os.remove() 调用的函数的奖励积分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-10
        • 2021-02-03
        • 2021-11-19
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 2017-08-28
        相关资源
        最近更新 更多