AttributeError:模块“ shutil”没有属性“ copy”

时间:2019-11-04 15:48:34

标签: python-3.5 shutil

我在python 3.5中遇到了有线错误。感谢您的帮助。

我想使用shutil将一些图像从一个目录复制到另一个目录。据我所知,shutil是一个标准库(不需要安装)。但是它不能正常工作。如何重新安装它或解决错误?

import shutil
import os
import random

##  This program copy some images of a folder randomly into an existed directory

src = "/FirstPath/BIODI_V3_175x100/Train_set/"
dist = "/SecondPath/Random_Images_of_BIODI_for_test/"
copied = 0
mlist = os.listdir(src)
#os.mkdir(dist)
for i in range(1000):
    rand = random.randint(1,len(mlist))
    shutil.copy((os.path.join(src,mlist[rand])),dist)
    i += 1

这是错误:

Traceback (most recent call last):
  File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 1, in <module>
    import shutil
  File "/usr/lib/python3.5/shutil.py", line 13, in <module>
    import tarfile
  File "/usr/lib/python3.5/tarfile.py", line 49, in <module>
    import copy
  File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 15, in <module>
    shutil.copy((os.path.join(src,mlist[rand])),dist)
AttributeError: module 'shutil' has no attribute 'copy'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 12, in <module>
    import subprocess, tempfile, os.path, re, pwd, grp, os, time
  File "/usr/lib/python3.5/tempfile.py", line 44, in <module>
    import shutil as _shutil
  File "/usr/lib/python3.5/shutil.py", line 13, in <module>
    import tarfile
  File "/usr/lib/python3.5/tarfile.py", line 49, in <module>
    import copy
  File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 15, in <module>
    shutil.copy((os.path.join(src,mlist[rand])),dist)
AttributeError: module 'shutil' has no attribute 'copy'

Original exception was:
Traceback (most recent call last):
  File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 1, in <module>
    import shutil
  File "/usr/lib/python3.5/shutil.py", line 13, in <module>
    import tarfile
  File "/usr/lib/python3.5/tarfile.py", line 49, in <module>
    import copy
  File "/home/eshan/PycharmProjects/MTL_BIODI_backup/copy.py", line 15, in <module>
    shutil.copy((os.path.join(src,mlist[rand])),dist)
AttributeError: module 'shutil' has no attribute 'copy'

0 个答案:

没有答案