Change output filename by adding a text in Python

4040
5
Jump to solution
11-27-2017 08:09 AM
DuminduJayasekera
New Contributor III

HI,

I need to add "_var86" to the output file in the code below. How can I achieve it in the code below. 

Thanks in advance.

import arcpy, sys 
from arcpy import env
from arcpy.sa import *

# Input data source
arcpy.env.workspace = r"C:/Users/Desktop/weekly_avg_project"
arcpy.env.overwriteOutput = True

# Set output folder
OutputFolder = r"C:/Users/Desktop/weekly_avg_project/output"

# Loop through a list of files in the workspace
NCfiles = arcpy.ListFiles("*.nc")

for filename in NCfiles:
print("Processing: " + filename)
inNCfiles = arcpy.env.workspace + "//" + filename
fileroot = filename[0:(len(filename)-3)]
TempLayerFile = "SM_amount"
outRaster = OutputFolder + "//" + fileroot
# Process: Make NetCDF Raster Layer
outfile = arcpy.MakeNetCDFRasterLayer_md(filename, "var86", "lon", "lat", TempLayerFile, "", "", "BY_VALUE")
proj_ras = arcpy.ProjectRaster_management(outfile, "C:/Users/Desktop/weekly_avg_project/project_ras","GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]", "NEAREST", "0.125 0.125", "NAD_1983_To_WGS_1984_1", "", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
# Process: Copy Raster
arcpy.CopyRaster_management(proj_ras, outRaster + ".tif", "", "", "", "NONE", "NONE", "")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
MitchHolley1
MVP Regular Contributor

I see... the below code may work.  

import arcpy, sys 
from arcpy import env
from arcpy.sa import *

# Input data source
arcpy.env.workspace = r"C:/Users/Desktop/weekly_avg_project"
arcpy.env.overwriteOutput = True

# Set output folder
OutputFolder = r"C:/Users/Desktop/weekly_avg_project/output"

# Loop through a list of files in the workspace
NCfiles = arcpy.ListFiles("*.nc")

for filename in NCfiles:
print("Processing: " + filename)
inNCfiles = arcpy.env.workspace + "//" + filename
fileroot = filename[:(len(filename)-3)] + '_var86'
#fileroot = filename[:-3] + '_var86' #I think this will work too- and is easier to read
....
...
..
.‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

5 Replies
MitchHolley1
MVP Regular Contributor

Do you need to just rename the output filename?  If so, it seems like the output layer is currently the 'TempLayerFile' variable.  You can rename that like the code below. 

import arcpy, sys 
from arcpy import env
from arcpy.sa import *

# Input data source
arcpy.env.workspace = r"C:/Users/Desktop/weekly_avg_project"
arcpy.env.overwriteOutput = True

# Set output folder
OutputFolder = r"C:/Users/Desktop/weekly_avg_project/output"

# Loop through a list of files in the workspace
NCfiles = arcpy.ListFiles("*.nc")

for filename in NCfiles:
print("Processing: " + filename)
inNCfiles = arcpy.env.workspace + "//" + filename
fileroot = filename[0:(len(filename)-3)]
TempLayerFile = "SM_amount_var86"
outRaster = OutputFolder + "//" + fileroot
# Process: Make NetCDF Raster Layer
outfile = arcpy.MakeNetCDFRasterLayer_md(filename, "var86", "lon", "lat", TempLayerFile, "", "", "BY_VALUE")
proj_ras = arcpy.ProjectRaster_management(outfile, "C:/Users/Desktop/weekly_avg_project/project_ras","GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]", "NEAREST", "0.125 0.125", "NAD_1983_To_WGS_1984_1", "", "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
# Process: Copy Raster
arcpy.CopyRaster_management(proj_ras, outRaster + ".tif", "", "", "", "NONE", "NONE", "")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DuminduJayasekera
New Contributor III

No. I need to change the file name for the "outRaster" not the "TempLayerFile"

0 Kudos
DuminduJayasekera
New Contributor III

I need to add "_var86" to the filename which will be the output name for the raster. 

0 Kudos
MitchHolley1
MVP Regular Contributor

I see... the below code may work.  

import arcpy, sys 
from arcpy import env
from arcpy.sa import *

# Input data source
arcpy.env.workspace = r"C:/Users/Desktop/weekly_avg_project"
arcpy.env.overwriteOutput = True

# Set output folder
OutputFolder = r"C:/Users/Desktop/weekly_avg_project/output"

# Loop through a list of files in the workspace
NCfiles = arcpy.ListFiles("*.nc")

for filename in NCfiles:
print("Processing: " + filename)
inNCfiles = arcpy.env.workspace + "//" + filename
fileroot = filename[:(len(filename)-3)] + '_var86'
#fileroot = filename[:-3] + '_var86' #I think this will work too- and is easier to read
....
...
..
.‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

DuminduJayasekera
New Contributor III

Thanks. it worked.

0 Kudos