[英]How to get Python to look at Sub-Folders?
I am trying to create a python script that will look in a series of sub-folders and delete empty shapefiles. 我正在尝试创建一个Python脚本,该脚本将在一系列子文件夹中查找并删除空的shapefile。 I have successfully created the part of the script that will delete the empty files in one folder, but there are a total of 70 folders within the "Project" folder.
我已经成功创建了脚本部分,该部分将删除一个文件夹中的空文件,但是“项目”文件夹中总共有70个文件夹。 While I could just copy and paste the code 69 times I'm sure must be a way to get it to look at each sub-folder and run the code for each of those sub-folders.
虽然我可以将代码复制和粘贴69次,但我确信这一定是一种方法,可以查看每个子文件夹并为每个子文件夹运行代码。 Below is the what I have so far.
以下是我到目前为止所拥有的。 Any ideas?
有任何想法吗? I'm very new to this and I have simply edited an existing code to get this far.
我对此很陌生,我只是编辑了一个现有代码就可以做到这一点。 Thanks!
谢谢!
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")
There are several ways to do this. 有几种方法可以做到这一点。 I'm a fan of
glob
我是
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)
Time to learn about procedural programming: Defining Functions . 是时候学习过程编程了: 定义函数 。
Put your code into a function with a path parameter and call it for each of your 70 paths: 将您的代码放入带有path参数的函数中,并为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)
Bonus points for creating a function that performs the os.path.exists()
and os.remove()
calls. 奖励点,用于创建执行
os.path.exists()
和os.remove()
调用的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.