Reputation: 21
I have thousands of file in the form tmp2010.m01.nc where 2010 is any year and 01 any month so for example (tmp1900.m02.nc, tmp1925.m12.nc, etc) are files.
I need the names to be tmp1900m02.nc and tmp1925m12.nc for use in a python script.
So basically, I need to know how to remove the extra "." between the year and the "m"
So far I've tried:
ren *.m*.nc ???????m*.nc
ren *.m*.nc *m*.nc
and neither of those have worked.
Or to solve the issue I'm running into in Python. If I don't rename it, and leave all of the files as is, python merges tmp1900.m01.nc, tmp1900.m02.nc,...,tmp1900.m12.nc into tmp1900.nc which is a problem since I need to have monthly files. The python script I'm using (which I know will work with tmp1900m01.nc type files is:
# Identify wet files
NCfiles = arcpy.ListFiles("wet*.nc")
# Process: Make & Save NetCDF Raster Layer
for filename in NCfiles:
fileroot = os.path.splitext(filename)[0]
outFile = OutputFolder + str(fileroot)+".lyr"
if os.path.exists(outFile):
print("File " + filename + " already exists, nothing will be done")
else:
print("Processing: " + filename)
inNCfiles = os.path.join(arcpy.env.workspace, filename)
fileroot = os.path.splitext(filename)[0]
LayerName = fileroot
outRaster = os.path.join(OutputFolder, fileroot)
inRaster = os.path.join(OutputFolder, fileroot + ".lyr")
arcpy.MakeNetCDFRasterLayer_md(inNCfiles, "wet", "lon", "lat", LayerName, "", "", "BY_VALUE")
arcpy.SaveToLayerFile_management(LayerName,outRaster,"ABSOLUTE")
Upvotes: 0
Views: 121
Reputation: 365767
The problem is in your Python script. Rather than try to bludgeon the input filenames to work around bugs in the script, let's just fix the script.
Your first problem is here:
fileroot = filename[0:(len(filename)-3)]
This looks like it's supposed to split off an extension. But, besides being horribly over-complicated (filename[0:(len(filename)-3)]
is exactly the same as filename[:-3]
), it's also wrong given that you have both 2- and 3-character extensions.
The os.path
module exists so you don't have to do all this stuff the hard way. Just call splitext
.
Next, you've got this:
LayerName = str(filename).strip(r"*.nc")
This looks like it's supposed to strip off another extension. It's overcomplicated for different reasons (on top of using strip
instead of splitext
, what exactly is str(filename)
supposed to do? Is filename
not already a string?), and it's also wrong for a different reason: your files do not end with "*.nc"
. You can't pass glob (wildcard) patterns to strip
. And, even if you could, wouldn't that just strip off the entire filename?
On top of that, instead of splitting the next extension off the stripped fileroot
, you're re-stripping the same extension off the original filename
.
So:
print("Processing: " + filename)
inNCfiles = os.path.join(arcpy.env.workspace, filename)
fileroot = os.path.splitext(filename)[0]
LayerName, m_ext = os.path.splitext(fileroot)
outRaster = os.path.join(OutputFolder, fileroot)
For the last line, I'm not sure whether you want the layer files to be tmp1900m01.lyr
but they're tmp1900.lyr
, or whether you're complaining that they're the former when you want the latter, or whether what you actually want is files like tmp1900.m01.lyr
, just like the .nc
files but with a different extension. But whichever one you want, you now have enough information. LayerName
has the tmp1900
part; m_ext
has the .m01
part, and fileroot
has the whole tmp1900.m01
. So, it's one of these:
inRaster = os.path.join(OutputFolder, LayerName + ".lyr")
inRaster = os.path.join(OutputFolder, LayerName + m_ext[1:] + ".lyr")
inRaster = os.path.join(OutputFolder, fileroot + ".lyr")
Meanwhile, you've added this to the code:
outFile = OutputFolder + str(fileroot)+".lyr"
Again, I don't know why you're calling str
on something that's already a string. And it's a bit confusing that you've got a file named inRaster
and a file named outFile
that are supposed to be the same thing (is it an input file, or an output file?). More importantly, I don't know why you're trying to calculate the exact same path in two different ways.
But obviously, as long as you do that, you have to get them both right. Having inRaster
correctly set to tmp1900.m01.lyr
doesn't do you any good if outFile
is tmp1900.lyr
and that's the name you actually use to open the file.
As a side note, whenever you're using os.path.exists
, you may be doing it wrong. It's often simpler and more robust to just try to open the file and deal with it if things go wrong. For example, if you just open
with mode x
, it will raise an exception if the file already exists. Just handle that exception and continue on to the next filename.
Upvotes: 2
Reputation: 13699
Just replace the first period with nothing
file_names = ["tmp1900.m02.nc", "tmp1925.m12.nc"]
for file_name in file_names:
new_file_name = file_name.replace('.', '', 1)
print new_file_name
Upvotes: 0