简体   繁体   中英

Saving files based on filename to individual folders

I have a list of 480 files and I would like to save them to 80 folders based on their filenames. Examples of the filenames are:

LT50300281984137PAC00_sr_band1.tif
LT50300281984137PAC00_sr_band2.tif
LT50300282007136PAC01_sr_band1.tif
LT50300282007136PAC01_sr_band2.tif
LT50300282002138LGS01_sr_band1.tif
LT50300282002138LGS01_sr_band2.tif

I want to save files that have matching characters in a slice of the first 21 characters [0:21] into folders that have that same name. For example:

LT50300281984137PAC00_sr_band1.tif
LT50300281984137PAC00_sr_band2.tif 

would go into folder titled LT50300281984137PAC00

and

LT50300282007136PAC01_sr_band1.tif
LT50300282007136PAC01_sr_band2.tif

would go into folder titled LT50300282007136PAC01

I have already created the folders using this code:

import arcpy
import os
#pathway where there are only 80 .tif files, one for each landsat scene
arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\Band 1'
#list of rasters in above pathway
list1 = arcpy.ListRasters("*.tif")
#output save pathway
mainpath=r'F:\Sheyenne\Atmospherically Corrected Landsat\Individual Scenes\Main'

#create folder for each landsatscene containing first 21 characters
for raster in list1:
    rasterName=raster[0:21]
    if raster.startswith(rasterName.split("_")[0]):
        final_path=os.path.join(mainpath,rasterName)
        os.makedirs(final_path)

and now I want to take every file that has 'band' in the name like I showed above and store it in the correct folders but this is where I am stuck

import arcpy, os

original_path = r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\Band 1'
#pathway where there are only 80 .tif files, one for each landsat scene
arcpy.env.workspace=original_path
#list of rasters in above pathway
list1 = arcpy.ListRasters("*.tif")
#output save pathway
main_path=r'F:\Sheyenne\Atmospherically Corrected Landsat\Individual Scenes\Main'

#create folder for each landsatscene containing first 21 characters
for raster in list1:

    source_path = os.path.join(original_path, raster)
    dir_name=split("_")[0]
    destination_path=os.path.join(main_path, dir_name)

    if not os.path.isdir(destination_path):
        os.makedirs(destination_path)

    destination_path = os.path.join(destination_path, raster)

    if not os.path.exists(destination_path):
        os.rename(source_path, destination_path)

Run this script in the same directory with your .tif files. It assumes that you already have the directories, as you mentioned in the question.

import glob
import os

for source in glob.glob("*band*.tif"):
    target = os.path.join( source[:21], source )
    os.rename(source, target)

Since you have already created the folder, you should be able to use the os.rename command (as shown in the link) inside the loop: How to move a file in Python

# Create a var to store the original path
original_path = r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\Band 1'
arcpy.env.workspace= original_path

#list of rasters in above pathway
list1 = arcpy.ListRasters("*.tif")

#output save pathway
mainpath=r'F:\Sheyenne\Atmospherically Corrected Landsat\Individual Scenes\Main'

#create folder for each landsatscene containing first 21 characters
for raster in list1:
    rasterName=raster[0:21]
    if raster.startswith(rasterName.split("_")[0]):
        final_path=os.path.join(mainpath,rasterName)
        os.makedirs(final_path)
        # create a path for the new file
        new_file = os.path.join(final_path, 'new_file_name.tif')
        # create a path for the old file
        old_file = os.path.join(original_path, raster)
        # rename (move) the old file to the new file
        os.rename(old_file, new_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM