如果字典中的标志为真,如何将文件从一个文件夹复制到另一个文件夹
How to copy files from the one folder to another if flag is True in dictionary
我有一本包含布尔值的字典
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'}]}
/home/ubuntu/ 中有 3 个名为 db1、db2、db3 的文件。
如果标志为真,我们需要从
/home/ubuntu/db1 to /home/ubuntu/test/db1
/home/ubuntu/db3 to /home/ubuntu/test/db3
使用shutil复制的正常方式,我做到了。在哪里给出标志条件
import shutil
import os
import glob
setupname = glob.glob("test1/*")
for root, dirs, files in os.walk("test1/"):
for filename in files:
print(filename)
shutil.copy2(src, dest)
您需要将 if 语句放在文件名循环的顶部:
for root, dirs, files in os.walk("test1/"):
for filename in files:
if test[filename][0]['flag'] == 'True':
print(filename)
shutil.copy2(src, dest)
我有一本包含布尔值的字典
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'}]}
/home/ubuntu/ 中有 3 个名为 db1、db2、db3 的文件。
如果标志为真,我们需要从
/home/ubuntu/db1 to /home/ubuntu/test/db1
/home/ubuntu/db3 to /home/ubuntu/test/db3
使用shutil复制的正常方式,我做到了。在哪里给出标志条件
import shutil
import os
import glob
setupname = glob.glob("test1/*")
for root, dirs, files in os.walk("test1/"):
for filename in files:
print(filename)
shutil.copy2(src, dest)
您需要将 if 语句放在文件名循环的顶部:
for root, dirs, files in os.walk("test1/"):
for filename in files:
if test[filename][0]['flag'] == 'True':
print(filename)
shutil.copy2(src, dest)