我有一堆这样的文件名:
LT50300281984137PAC00_sr_band3.tif
LT50300281985137PAC00_sr_band1.tif
我想将每个文件名的9:16中包含的公历日期更改为公历日期,然后将新日期重新插入到文件名中。我已经使用以下代码将julian转换为gregorian:
import datetime, glob, os
for raster in glob.glob('r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\julian_dates/*.tif'):
year=int(oldFilename[9:13])
#the day
day=int(oldFilename[13:16])
#convert to julian date
date=datetime.datetime(year,1,1)+datetime.timedelta(day)
print date
这将为我提供每个文件的公历日期,因此对于像这样的LT50300281984137PAC00_sr_band3.tif
文件,它将返回1984-05-17 00:00:00
,但我不想要00:00:00
,我想将公历日期重新插入到文件名中,最好是19840517
。
编辑:
使用到目前为止所有答案中的建议,我可以使用以下代码执行重命名(此示例中的最后一行代码)之外的所有操作:
import datetime, glob, os
for raster in glob.glob(r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\julian_dates/*.tif'):
oldFilename=raster
year=int(oldFilename[9:13])
#the day
day=int(oldFilename[13:16])
#convert to julian date
date=datetime.datetime(year,1,1)+datetime.timedelta(day)
#generate newfile names
newFilename=oldFilename[:9] + date.strftime('%Y%m%d') + oldFilename[16:]
#rename the files
os.rename(oldFilename, newFilename)
这将返回error:WindowsError: [Error 2] The system cannot find the file specified
,我认为这可能与我的os路径有关。在此之前,所有其他变量都会按预期进行填充。
编辑:此代码适用于我
arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\julian_dates'
hank_bands='F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\julian_dates'
hank_out='F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\greg_dates'
list1=arcpy.ListRasters("*.tif")
for raster in list1:
source_path = os.path.join(hank_bands, raster)
oldFilename=raster
year=int(oldFilename[9:13])
#the day
day=int(oldFilename[13:16])
#convert to julian date
date=datetime.datetime(year,1,1)+datetime.timedelta(day)
newFilename=oldFilename[:9] + date.strftime('%Y%m%d') + oldFilename[16:]
destination_path=os.path.join(hank_out, newFilename)
os.rename(source_path, destination_path)
发布于 2015-08-14 19:52:15
为此,您可以使用regex:
import re
import os
filename = 'LT50300281984137PAC00_sr_band3.tif'
oldDate = re.sub('(LT5030028)|(PAC00_sr_band3.tif)','',filename) # Extracts the old date
# calculate new date from old date
# newDate = '1984-05-17 00:00:00'
newDate = re.sub('(-)|( .*)','',newDate) # Removes the dashes and the time
newFilename = filename.replace(oldDate,newDate) # Replaces the old date by the new date
os.rename(filename, newFilename) # renames the file to the new file name
发布于 2015-08-14 20:12:22
一旦你有了year
和day
,strftime
方法就会给出你的结果。对于1984和137,你会得到:
import datetime
date=datetime.date(year,1,1)+datetime.timedelta(day)
printf(date.strftime("%4Y%2m%2d"))
19840517
因此,您现在可以这样做:
newFilename = oldFilename[:9] + date.strftime("%4Y%2m%2d") + oldFilename[16:]
发布于 2015-08-14 21:48:13
要将字符串转换为date对象,请使用datetime.strptime()
>>> from datetime import datetime
>>> datetime.strptime('1985137', '%Y%j')
datetime.datetime(1985, 5, 17, 0, 0)
>>> datetime.strptime('1984137', '%Y%j')
datetime.datetime(1984, 5, 16, 0, 0)
注意:输入有不同的解释:1984137
在这里是1984-05-16
(137
被解释为一年中的某一天,其中1月1日是第1天),而您问题中的datetime(year, 1, 1) + timedelta(day)
公式暗示day
是从零开始的(在两种情况下,2月29日都被计算在内)。
要将date对象转换为字符串,请使用.strftime()
方法:
>>> datetime(1985, 5, 17, 0, 0).strftime('%Y%m%d')
'19850517'
要替换文件名中的固定位置,请执行以下操作:
>>> filename = 'LT50300281984137PAC00_sr_band3.tif'
>>> filename[9:16]
'1984137'
>>> new_name = filename[:9] + '19840516' + filename[16:]
>>> new_name
'LT503002819840516PAC00_sr_band3.tif'
如果目标可能位于不同的文件系统上,则要重命名文件,请使用shutil.move()
>>> import shutil
>>> shutil.move(os.path.join(src_dir, filename), os.path.join(dest_dir, new_name))
如果目标文件可能已经存在,则调用os.remove(dest_path)
。
https://stackoverflow.com/questions/32017624
复制