简体   繁体   English

如何将新文件保存到其他目录

[英]How to save new file into an other directory

I would like to save few elements in new directories.我想在新目录中保存一些元素。 The differents files are classified by type.不同文件按类型分类。 I want to use the different id of each one, to save them in a new directory (one directory for each class).我想使用每个不同的 id,将它们保存在一个新目录中(每个类一个目录)。

Listes = [index_birch, index_maple, index_asp, index_ash, index_oak]
Liste_name = ["index_birch", "index_maple", "index_asp", "index_ash"," index_oak"]
path_final = r"C:\Desktop\Université_2019_2020\CoursS2_Mosef\Stage\Data\Grand_Leez\shp\Test_sur_newdta\Test_prediction"
    


list_index_fi = os.listdir(r"C:\Desktop\Université_2019_2020\CoursS2_Mosef\Stage\Data\Grand_Leez\shp\Test_sur_newdta\Test_prediction")`

    itera=0
    for liste in Liste_name:
        new_path = Liste_name[itera]
        dir_name = os.path.join(path_final,new_path)
        os.makedirs(dir_name)
        itera+=1
        for i, npfile in enumerate(Listes):
            value = npfile
            for j, k in enumerate(list_index_fi):
                if value in k:
                    shutil.move(os.path.join(path_final,j), dir_name)
                else:
                     pass

When i use this code i have the message: 'in string' requires string as left operand, not list当我使用此代码时,我收到消息:“in string”需要字符串作为左操作数,而不是列表

I know that my error come from the fact that the variable value is a list of string (with Listes == list of list) and not a string.我知道我的错误来自这样一个事实,即变量是一个字符串列表(Listes == list of list)而不是字符串。 How I am suppose to iterate over my list to see if its elements correspond to a certain value of my os.listdir command?我应该如何遍历我的列表以查看其元素是否对应于我的 os.listdir 命令的某个值?

NB: Listes = list of list注意:列表=列表列表

0
['3007', '3008', '3012', '3020', '3022', '3023', '3024', '3027', '3029', '3032', '3033', '3035', '3047', '3050', '3056', '3065', '3066', '3079', '3080', '3089', '3090', '3098']
1
['3000', '3001', '3006', '3011', '3013', '3025', '3026', '3028', '3036', '3043', '3053', '3059', '3060', '3061', '3074', '3077', '3082', '3083', '3085', '3094']
2
[]
3
[]
4
['3002', '3003', '3004', '3005', '3009', '3010', '3014', '3015', '3016', '3017', '3018', '3019', '3021', '3030', '3031', '3034', '3037', '3038', '3039', '3040', '3041', '3042', '3044', '3045', '3046', '3048', '3049', '3051', '3052', '3054', '3055', '3057', '3058', '3062', '3063', '3064', '3067', '3068', '3069', '3070', '3071', '3072', '3073', '3075', '3076', '3078', '3081', '3084', '3086', '3087', '3088', '3091', '3092', '3093', '3095', '3096', '3097', '3099']

Thanks谢谢

You can simply iterate over the main list to get the inner list and then iterate over inner list and do operations that you want to do.您可以简单地遍历主列表以获取内部列表,然后遍历内部列表并执行您想要执行的操作。 If the error is that it requires a string.如果错误是它需要一个字符串。 You can check the type of the input variable provided.您可以检查提供的输入变量的类型。 It should output the input type as list as of now however input type string should be correct.它应该 output 输入类型作为现在的列表,但是输入类型字符串应该是正确的。

I finally found the solution of my problem by using a dictionnary.我终于通过使用字典找到了我的问题的解决方案。

Listes = [index_birch, index_maple, index_asp, index_ash, index_oak]
Liste_name = ["index_birch", "index_maple", "index_asp", "index_ash"," index_oak"]
path_final = r"C:\Desktop\Université_2019_2020\CoursS2_Mosef\Stage\Data\Grand_Leez\shp\Test_sur_newdta"

list_index_fi = glob.glob(r"C:\Desktop\Université_2019_2020\CoursS2_Mosef\Stage\Data\Grand_Leez\shp\Test_sur_newdta\*.gpkg")

Dict = dict()
i=0
for liste in Liste_name:
    dir_name = os.path.join(path_final,liste)
    if not os.path.exists(dir_name):        
        os.makedirs(dir_name)
    Dict[dir_name] = Listes[i]
    i+=1



listeIndex = []
for index in list_index_fi:
    names = index.split('\\')[-1].replace("id_","").replace(".gpkg", "")
    listeIndex.append(names)

for key, value in Dict.items():
    print(key)
    print(value)
    #print value
    for elem in value:
        print('--------------')
        print("ELEMENT", elem)

        if elem in listeIndex:
            print("**** OK ****")
            ind = listeIndex.index(elem)
            file_to_move = list_index_fi[ind]
            print(file_to_move)
            print(key)
            shutil.move(file_to_move, key)
        else:
            pass

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

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