![](/img/trans.png)
[英]Python [WinError 32] with shutil.move(source, destination) during the move (writes most of the file, but not all)
[英]shutil.move() - error message that the destination file does not exist
我有以下代碼將CSV文件排序為類似於創建CSV文件的WAV文件的目錄結構:
from __future__ import print_function
import os
import shutil
WAV_FILES_PATH = 'g:\\wav_files\\test007\\'
CSV_FILES_PATH = 'g:\\csv_files\\test007\\'
wav_files_path = os.walk(WAV_FILES_PATH)
csv_files_path = os.walk(CSV_FILES_PATH)
# I'm only interested in CSV files in the root for CSV_FILES_PATH
(csv_root, _, csv_files) = csv_files_path.next()
print('Running ...')
for root, subs, files in wav_files_path:
for file_ in files:
if file_.endswith('wav'):
for csv_file in csv_files:
if(file_.split('.')[0] in csv_file):
src = os.path.join(csv_root, csv_file)
dst = os.path.join(csv_root, root.replace(WAV_FILES_PATH, ''), csv_file)
print('Moving "%s" to "%s" ...' % (src, dst))
shutil.move(src, dst)
WAV_FILES_PATH中的子文件夾包含WAV文件,例如
g:\wav_files\test007\run001\
g:\wav_files\test007\run002\
由於CSV文件在g:\\csv_files\\test007
中位於無序g:\\csv_files\\test007
,因此我想克隆目錄結構並將CSV文件移動到其正確的文件夾中。 最后,我想擁有如g:\\csv_files\\test007\\run001\\
包含對應於WAV文件的CSV g:\\wav_files\\test007\\run001\\
。
問題是shutil.move()
命令給我一個IOError [Errnor 2]
抱怨DESTINATION不存在。 這使我感到困惑,因為我對目標具有寫訪問權,shutil.move()聲稱目標目錄不必存在。
我在這里想念什么嗎?
print()函數可正確打印出src和dst。
這是錯誤輸出:
[...]
C:\Python27\lib\shutil.pyc in copyfile(src, dst)
80 raise SpecialFileError("`%s` is a named pipe" % fn)
81
82 with open(src, 'rb') as fsrc:
---> 83 with open(dst, 'wb') as fdst:
84 copyfileobj(fsrc, fdst)
IOError: [Errno 2] No such file or directory: 'g:\\csv_files\\test007\\run001\\recording_at_20140920_083721.csv'
信息:我將錯誤拋出部分( with
塊)直接添加到了我的代碼中,並且沒有拋出錯誤。 現在,我將自己復制文件,然后將其刪除。
這似乎是shutil.move()操作方式的錯誤。
我對您的代碼做了些微修改,我認為它會產生預期的結果。
#dst = os.path.join(csv_root, root.replace(WAV_FILES_PATH, ''), csv_file)
dst = os.path.join(csv_root, root.replace(WAV_FILES_PATH, '')) # Modified
在執行shutil.move()之前,我還在這里添加了以下邏輯enter code here
if not os.path.exists(dst):
os.makedirs(dst)
希望它也能為您服務!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.