Python:FileNotFoundError:[Errno 2]没有这样的文件或目录

时间:2019-07-15 16:55:26

标签: python filenames shutil

如果字典中的标志为True,我正在将文件从一个文件夹复制到另一个文件夹。

源文件夹:/home/ubuntu/test1

目标文件夹:/home/ubuntu/test2

我遇到此错误:

File "/home/ubuntu/anaconda3/lib/python3.7/shutil.py", line 263, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/home/ubuntu/anaconda3/lib/python3.7/shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:


FileNotFoundError: [Errno 2] No such file or directory: 'db1'

我的代码是:

import shutil
import os

test = {'db1': [{'url': 'http://localhost:8080/api', 'cmd': 'test\\nshow databases', 'flag': 'True'}],
        'db2': [{'url': 'http://localhost:8080/api', 'cmd': 'test\\nshow databases', 'flag': 'False'}],
        'db3': [{'url': 'http://localhost:8080/api', 'cmd': 'test\\nshow databases', 'flag': 'True'}]}

destination = "test2/"
for root, dirs, files in os.walk("test1/"):
    for filename in files:
        if test[filename][0]['flag'] == 'True':
            print(filename)
            #os.chdir('test1/')
            shutil.copy2(filename, destination)

0 个答案:

没有答案