1

ある既存のディレクトリから別の既存のディレクトリに複数のファイルを移動することに関連する多くの投稿があります。残念ながら、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 つだけ必要です。それだけです...

4

2 に答える 2

1

shutil.movefは、ソース ディレクトリではなく、現在の作業ディレクトリを探している可能性があります。フルパスを指定してみてください。

for f in files:
    if (f.endswith("harvest.out")):
        shutil.move(os.path.join(source, f), dest1)
    elif (f.endswith("sow.out")):
        shutil.move(os.path.join(source, f), dest2)
于 2015-09-01T19:01:37.053 に答える
0

これは機能するはずです。そうでない場合はお知らせください:

import os
import shutil

srcdir = r'G:\Tables\\'
destdir = r'G:\Tables\Soil'

for filename in os.listdir(path):
    filepath = os.path.join(path, filename)

    with open(filepath, 'r') as f:
        dataname = f.name

    print dataname

    shutil.move(fullpath, dest)
于 2015-09-01T19:01:58.807 に答える