2017-06-13 26 views
0

enter image description hereIOError:[Errno 2]そのようなファイルやディレクトリはありませんか?

私は、ファイルのパッケージをtrain_reutersする小麦で220個のファイルを移動しようとしている、とtrain_reuters test_reutersファイルパッケージに小麦移動中の別のファイルが、私は、コードを実行すると、それは私にエラーを与え、実際に私です適切な場所にファイルを持ってください!どうすれば問題を解決できますか? enter image description here

#!/usr/bin/python 
#coding:utf-8 
import sys 
reload(sys) 
sys.setdefaultencoding('utf-8') 
import os 
import os.path 
import shutil 
import random 
path = '/home/user1/zhouchun/lda/reuters-21578/Wheat' 
targetpath1 = '/home/user1/zhouchun/lda/reuters-21578/train_reuters' 
targetpath2 = '/home/user1/zhouchun/lda/reuters-21578/test_reuters' 
list=random.sample(range(1, 306),220) 
for i in list: 
    file_dir = os.path.join(path, str(i)) 
    # print file_dir 
    shutil.move(file_dir, targetpath1) 
files = os.listdir(path) 
for file in files: 
    # print file 
    dir = os.path.join(path, file) 
    if dir != file_dir: 
     shutil.move(dir, targetpath2) 

答えて

0

私はそれが正しい、あなたのコードをチェックします。 次に、問題が発生する可能性があります。 1.コードを1回だけ実行すると、2回以上このエラーが発生します。 2.コードを実行する前に、すべての306のファイルがWheatディレクトリにあることを確認してください。

私はコピーを使用することを推奨しますが、実行する前に列車とテストファイルを移動してからクリアしないでください。

0

ome/user1/zhouchun/lda/reuters-21578/Wheatファイルの数が、私は関数はランダムなファイル、あなたが参照できるコードを書く作成305

でご確認ください。

import random 
import os 

path = r'E:\temp\temp' 

list= random.sample(range(1, 306), 220) 
for i in list: 
    file_dir = os.path.join(path, str(i)) 
    with open(file_dir, 'w') as f: 
     f.write('file_dir: %s' % file_dir) 
     f.close() 

ラインlist= random.sample(range(1, 306), 220)220に気付いてください。

した後、このコードを実行し、エラー情報が収入がする

#!/usr/bin/python 
#coding:utf-8 
import sys 

import os.path 
import shutil 
import random 
import time 

path = r'E:\temp\temp' 
targetpath1 = r'E:\temp\old' 
targetpath2 = r'E:\temp\new' 

# move the file  
list = random.sample(range(1, 306), 220) 
for i in list: 
    file_dir = os.path.join(path, str(i)) 
    # print file_dir 
    # targetpath1_dir = os.path.join(targetpath1, str(i)) 
    shutil.move(file_dir, targetpath1) 
files = os.listdir(path) 


for file in files: 
    # print(file) 
    # print file 
    dir = os.path.join(path, file) 

    if dir != file_dir: 
     shutil.move(dir, targetpath2) 

よりも、あなたのコードを貼り付け、パスを変更します。ラインlist= random.sample(range(1, 306), 220)220から305に番号を変更した後

Traceback (most recent call last): 
    File "D:\Python_3.5\lib\shutil.py", line 544, in move 
    os.rename(src, real_dst) 
FileNotFoundError: [WinError 2] System can't found the file.: 'E:\\temp\\temp\\182' -> 'E:\\temp\\old\\182' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "F:/Python_Code/FaceDetect/123123.py", line 31, in <module> 
    shutil.move(file_dir, targetpath1) 
    File "D:\Python_3.5\lib\shutil.py", line 558, in move 
    copy_function(src, real_dst) 
    File "D:\Python_3.5\lib\shutil.py", line 257, in copy2 
    copyfile(src, dst, follow_symlinks=follow_symlinks) 
    File "D:\Python_3.5\lib\shutil.py", line 120, in copyfile 
    with open(src, 'rb') as fsrc: 
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\temp\\temp\\182' 

、エラーが表示されなくなります。

完全なコード。

#!/usr/bin/python 
#coding:utf-8 
import sys 

import os.path 
import shutil 
import random 
import time 

path = r'E:\temp\temp' 
targetpath1 = r'E:\temp\old' 
targetpath2 = r'E:\temp\new' 

# create the random file. 
list= random.sample(range(1, 306), 220) 
for i in list: 
    file_dir = os.path.join(path, str(i)) 
    with open(file_dir, 'w') as f: 
     f.write('file_dir: %s' % file_dir) 
     f.close() 

time.sleep(1) 

# move the file 

list = random.sample(range(1, 306), 220) 
for i in list: 
    file_dir = os.path.join(path, str(i)) 
    # print file_dir 
    # targetpath1_dir = os.path.join(targetpath1, str(i)) 
    shutil.move(file_dir, targetpath1) 
files = os.listdir(path) 


for file in files: 
    # print(file) 
    # print file 
    dir = os.path.join(path, file) 

    if dir != file_dir: 
     shutil.move(dir, targetpath2) 

参考にしてください。

関連する問題