简体   繁体   中英

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).

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

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. How I am suppose to iterate over my list to see if its elements correspond to a certain value of my os.listdir command?

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.

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

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