简体   繁体   English

根据文件名将文件保存到单个文件夹

[英]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. 我有480个文件的列表,我想根据它们的文件名将它们保存到80个文件夹中。 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. 我想将前21个字符[0:21]的一部分中具有匹配字符的文件保存到具有相同名称的文件夹中。 For example: 例如:

LT50300281984137PAC00_sr_band1.tif
LT50300281984137PAC00_sr_band2.tif 

would go into folder titled LT50300281984137PAC00 将进入名为LT50300281984137PAC00文件夹

and

LT50300282007136PAC01_sr_band1.tif
LT50300282007136PAC01_sr_band2.tif

would go into folder titled LT50300282007136PAC01 将进入名为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 现在我要像上面显示的那样取每个带有“ band”名称的文件,并将其存储在正确的文件夹中,但这就是我遇到的问题

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. 在与.tif文件相同的目录中运行此脚本。 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 由于已经创建了文件夹,因此您应该能够在循环内使用os.rename命令(如链接所示): 如何在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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