Python 3.x 我想保存新的txt,但不包括旧的。有12个旧的txt。循环结束后如何保存12个新循环
我在一个特定的目录中有一定数量的文件,我想在对它们执行某些操作后将其保存为一个新文件。 我尝试这样做的实际代码是:Python 3.x 我想保存新的txt,但不包括旧的。有12个旧的txt。循环结束后如何保存12个新循环,python-3.x,Python 3.x,我在一个特定的目录中有一定数量的文件,我想在对它们执行某些操作后将其保存为一个新文件。 我尝试这样做的实际代码是: fileList=glob.glob('MODIS_temp_2010./*.txt') f_len=len(fileList) for k in range(0,f_len): filename=fileList[k] modis_temp=np.loadtxt(filename) modis_temp=np.array(modis_temp) modis_tem
fileList=glob.glob('MODIS_temp_2010./*.txt')
f_len=len(fileList)
for k in range(0,f_len):
filename=fileList[k]
modis_temp=np.loadtxt(filename)
modis_temp=np.array(modis_temp)
modis_temp=np.where(modis_temp==0,np.nan,modis_temp)
modis_temp=modis_temp*0.02
modis_temp=modis_temp-273.15
np.savetxt('modis_temp.txt',modis_temp)
plt.imshow(modis_temp)
我需要如何更改才能正常工作?从我的理解来看,您希望在同一目录中修改文件后将其保存。 因为您希望将新文件保存在同一目录中,因为操作系统和文件系统的需要,它们必须是可区分的。这是通过使用不同的名称来完成的,如果我们在一个特定的目录中,或者对同一个名称文件使用不同的目录。 在您的情况下,我们需要创建一个新名称,您可以附加例如当前日期,指示编辑时间,以便新的12个文件将保留旧文件
import datetime
today = datetime.datetime.now() #current date
date = today.strftime('%d_%m_%Y')
newname = filename.split('.')[0] #we are taking only the name not the extension part
newname += date
现在,您有了一个新的文件名,指明了文件的编辑时间。一个更好的解决方案是创建一个新目录,将编辑日期作为名称,并写入文件,以便它们保持相同的名称