【问题标题】:IOError: [Errno 2] No such file or directory?IOError: [Errno 2] 没有这样的文件或目录?
【发布时间】:2017-06-13 11:40:57
【问题描述】:

我正在尝试将 Wheat 中的 220 个文件移动到 train_reuters 文件包,wheat 中的另一个文件移动到 train_reuters test_reuters 文件包,但是当我运行代码时,它给了我错误,我实际上有文件正确的地方!我该如何解决这个问题?

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

【问题讨论】:

    标签: python


    【解决方案1】:

    我检查了您的代码,它是正确的。 那么问题可能是: 1.你的代码只能运行一次,两次或更多次会导致这个错误。 2. 运行代码前,确保所有306文件都在Wheat目录下。

    我建议使用复制,但不要移动,然后在每次运行前清除训练和测试文件。

    【讨论】:

      【解决方案2】:

      请检查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)
      

      比运行代码,错误信息会收入。

      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'
      

      list= random.sample(range(1, 306), 220)行中的数字从220更改为305后,错误就会消失。

      完整的代码。

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

      请参考。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-26
        相关资源
        最近更新 更多