2017-06-13 116 views
0

enter image description hereIOError:[Errno 2]沒有這樣的文件或目錄?

我試圖移動220個文件小麥train_reuters文件包,並在小麥舉動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.你只能運行你的代碼一次,兩次或更多次會導致這個錯誤。 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() 

請注意行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) 

比運行代碼,錯誤信息將收入。

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' 

變化從220305符合list= random.sample(range(1, 306), 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' 

# 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) 

請參考。

相關問題