ある既存のディレクトリから別の既存のディレクトリに複数のファイルを移動することに関連する多くの投稿があります。残念ながら、Windows 8 と Python 2.7 ではまだうまくいきません。
私の最善の試みは、このコードを使用しているようですが、shutil.copy
正常に動作するshutil.move
ためです。
WindowsError: [エラー 32] 別のプロセスで使用されているため、プロセスはファイルにアクセスできません
import shutil
import os
path = "G:\Tables\"
dest = "G:\Tables\Soil"
for file in os.listdir(path):
fullpath = os.path.join(path, file)
f = open( fullpath , 'r+')
dataname = f.name
print dataname
shutil.move(fullpath, dest)
del file
del file
ファイルを閉じていないことが問題であることはわかっていますが、との両方で既に試しましたclose.file()
。
私が試した別の方法は次のとおりです。
import shutil
from os.path import join
source = os.path.join("G:/", "Tables")
dest1 = os.path.join("G:/", "Tables", "Yield")
dest2 = os.path.join("G:/", "Tables", "Soil")
#alternatively
#source = r"G:/Tables/"
#dest1 = r"G:/Tables/Yield"
#dest2 = r"G:/Tables/Soil"
#or
#source = "G:\\Tables\\Millet"
#dest1 = "G:\\Tables\\Millet\\Yield"
#dest2 = "G:\\Tables\\Millet\\Soil"
files = os.listdir(source)
for f in files:
if (f.endswith("harvest.out")):
shutil.move(f, dest1)
elif (f.endswith("sow.out")):
shutil.move(f, dest2)
os.path.join (「G:」または「G:/」のいずれかを使用) を使用すると、
WindowsError: [Error 3] The system cannot find the path specified:
'G:Tables\\Yield/*.*',
スラッシュ ( source = r"G:/Tables/"
)を使用すると、
IOError: [Errno 2] No such file or directory: 'Pepper_harvest.out'*
、
あるフォルダから別のフォルダにファイルを移動する方法が 1 つだけ必要です。それだけです...