第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將多個圖像從一個文件夾移動到python中的另一個文件夾?

如何將多個圖像從一個文件夾移動到python中的另一個文件夾?

慕慕森 2022-06-28 16:05:36
我正在嘗試使用 將多個圖像從一個文件夾移動到另一個文件夾shutil.move(),我已將圖像名稱保存在 CSV 文件中。前任:[img1 , img25, img55....]我試過下面的代碼import pandas as pdimport shutilcop_folder = path to folder containing images    destination_folder = path wher i want to move the imagesdf = pd.read_csv('', header = None)    for i in df:           if i in cop_folder:        shutil.move( i, dest_folder)    else:        print('fail')TypeError: 'in' 需要字符串作為左操作數(shù),而不是 int
查看完整描述

2 回答

?
烙印99

TA貢獻1829條經(jīng)驗 獲得超13個贊

試試這個方法:


import pandas as pd

import os



def move_file(old_file_path, new_directory):

    if not os.path.isdir(new_directory):

        os.mkdir(new_directory)

    base_name = os.path.basename(old_file_path)

    new_file_path = os.path.join(new_directory, base_name)

    # Deletes a file if that file already exists there, you can change this behavior

    if os.path.exists(new_file_path):

        os.remove(new_file_path)

    os.rename(old_file_path, new_file_path)



cop_folder = 'origin-folder\\'


destination_folder = 'dest_folder\\'


df = pd.read_csv('files.csv', header=None)


for i in df[0]:

    filename = os.path.join(cop_folder, i)

    move_file(filename, destination_folder)

csv 中的文件名必須有擴展名。如果他們沒有,那么你應該使用 filename = os.path.join(cop_folder, i + '.jpg')


查看完整回答
反對 回復 2022-06-28
?
長風秋雁

TA貢獻1757條經(jīng)驗 獲得超7個贊

這里有一些問題,首先您正在迭代一個數(shù)據(jù)框,該數(shù)據(jù)框將返回列標簽而不是值 - 這就是導致您發(fā)布的錯誤的原因。如果您真的想使用 pandas 來導入 CSV,那么您可以將其更改為for i in df.iterrows()但即使這樣它也不會簡單地返回文件名,它會返回一個系列對象。使用標準 CSV 模塊讀取 CSV 可能會更好。這樣,您的文件名將作為列表讀入,并按照您的預期運行。

其次,除非您的代碼中有其他內(nèi)容,否則您無法使用“in”關鍵字在文件夾中查找文件,您需要通過連接文件名和文件夾路徑來構建完整的文件路徑。


查看完整回答
反對 回復 2022-06-28
  • 2 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號