ArcGIS & python学习:(一)批处理修改文件名(NDVI数据)
下载的NDVI数据,一共26年,每年有24个文件夹(每月2个),每个文件夹下3个文件,文件名很长,类似这样的:“Subset_China_EA85aug15b.n09-VIg_albers_TIFF.tif”,看着太累,而且之后处理需要每年的放在一个文件夹下,批量修改文件名容易出错且累死,用python很快可以做到。
折腾了1天,总算倒腾出来了,先记录一下(环境:ArcGIS10.0,python2.6):
# -*- coding: cp936 -*-
import os.path
from os.path import join, getsize
print "----------------------Rename Begin--------------------------"
srcdir = "F://other//pythontest//2006"
for root, dirs, files in os.walk(srcdir):
for file in files:
#文件名(除去后缀名)
filename = os.path.splitext(file)[0][0:]
#后缀名
sufix = os.path.splitext(file)[1]
#获取年份、月份、上或下月ab
year = filename[15:-26]
month = filename[17:-23]
updownmonth = filename[22:-20]
#print "year is ", year
#print "month is ", month
#print "updownmonth is ", updownmonth
if month == "jan":
newmonth = "01"
elif month == "feb":
newmonth = "02"
elif month == "mar":
newmonth = "03"
elif month == "apr":
newmonth = "04"
elif month == "may":
newmonth = "05"
elif month == "jun":
newmonth = "06"
elif month == "jul":
newmonth = "07"
elif month == "aug":
newmonth = "08"
elif month == "sep":
newmonth = "09"
elif month == "oct":
newmonth = "10"
elif month == "nov":
newmonth = "11"
elif month == "dec":
newmonth = "12"
else:
print "*****未找到当前月份*******"
#新的名称
tempname = year + newmonth + updownmonth
destfile = srcdir +"//"+ tempname +sufix
file = os.path.join(srcdir,file);
print "destfile is ",destfile
#print "file is ",file
#更改名称
os.rename(file, destfile)
print "-----------------------end----------------------------"
修改后,文件名缩短了,看着舒服多了。
下一步,再研究拷贝文件,把每年24个半月文件放在一个目录下,然后批量实现ArcGIS下的MVC最大合成,裁剪等。
转载自:https://mtr-1.oss-cn-beijing.aliyuncs.com/qyblog/2019/04/40379213.jpg