文件存在但无法使用Shutil复制

时间:2019-05-24 12:01:07

标签: python

我已经不知道为什么shutil不会复制我存在的BBBSFF.txt。有什么想法吗?

我什至可以使用打印来查找文件...

import os, shutil

source = os.listdir(r'C:\Users\vijo9001\Desktop\INFACT')

set = {
"AAA", "BBB", "CCC", "DDD", "EEE"
       }

for file in source:
     for x in set:
        if "SFF" in file: shutil.copy(file,r'C:\Users\vijo9001\Desktop\AAA\SFF')
        else:
            if (str(x)) in file:
                print(file)

错误代码:

AAA.txt
BBB.txt
  File "C:/Users/vijo9001/Desktop/Other/PyCharm/Tests/Current Test.py", line 12, in <module>
    if "SFF" in file: shutil.copy(file,r'C:\Users\vijo9001\Desktop\INFACT\SFF')
  File "C:\Program Files (x86)\Python37-32\lib\shutil.py", line 245, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Program Files (x86)\Python37-32\lib\shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'BBBSFF.txt'

Process finished with exit code 1

1 个答案:

答案 0 :(得分:1)

代码如下:

import os
import shutil

base_path = 'C:/Users/vijo9001/Desktop/INFACT'
source = os.listdir(base_path)

my_set = { "AAA", "BBB", "CCC", "DDD", "EEE" }

for file in source:
    full_path = base_path + '/' + file
    for x in my_set:
        if "SFF" in file:
            shutil.copy(full_path, 'C:/Users/vijo9001/Desktop/AAA/SFF')
        else:
            if (str(x)) in file:
                print(file)