簡體   English   中英

如何將新文件保存到其他目錄

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

我想在新目錄中保存一些元素。 不同文件按類型分類。 我想使用每個不同的 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

當我使用此代碼時,我收到消息:“in string”需要字符串作為左操作數,而不是列表

我知道我的錯誤來自這樣一個事實,即變量是一個字符串列表(Listes == list of list)而不是字符串。 我應該如何遍歷我的列表以查看其元素是否對應於我的 os.listdir 命令的某個值?

注意:列表=列表列表

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']

謝謝

您可以簡單地遍歷主列表以獲取內部列表,然后遍歷內部列表並執行您想要執行的操作。 如果錯誤是它需要一個字符串。 您可以檢查提供的輸入變量的類型。 它應該 output 輸入類型作為現在的列表,但是輸入類型字符串應該是正確的。

我終於通過使用字典找到了我的問題的解決方案。

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