根据部分文件名搜索和移动文件

时间:2018-10-26 11:43:01

标签: python shutil

因此,我是一家软件公司的质量控制人员,目前我只能使用Python脚本编写代码,以对108,00多个错误日志进行排序。我正在尝试做的是过滤掉来自我们以前和当前版本(分别为4.1.468和4.1.478)的报告。

显示的报告具有以下文件名(对于Mac Reports为MD,对于Windows Reports为WD):

4.1.468MD.OutOfBoundsException.RaiseOutOfBoundsException.1

脚本的第一部分有效,查找并创建文件夹(如果尚不存在)以将所选报告复制到该文件夹​​。但是报告的实际复制从未发生。

您所能提供的任何建议或指示都将受到欢迎。

import os
import shutil 
import time
import pprint
import csv
from collections import Counter

path = 'C:\Heavy Logging Reports\Recorded Issues'

names = os.listdir(path)


folder_name = ['468','478']
for x in range(0,2):
    if not os.path.exists(path+folder_name[x]):
        os.makedirs(path+folder_name[x])

dest1 = 'C:\Heavy Logging Reports\Recorded Issues468'
dest2 = 'C:\Heavy Logging Reports\Recorded Issues478'


for f in names:
    if (f[:4].startswith("468WD")):
        shutil.copy(path, dest1)

    elif (f[:4].startswith("478WD")):
        shutil.copy(path, dest2)


print('Finished Moving Files!')

1 个答案:

答案 0 :(得分:0)

这应该有帮助。

演示:

import os
import shutil 
import time
import pprint
import csv
from collections import Counter

path = r'C:\Heavy Logging Reports\Recorded Issues'

names = os.listdir(path)


folder_name = ['468','478']
for x in folder_name:
    fPath = path+x
    if not os.path.exists(fPath):
        os.makedirs(fPath)

dest1 = 'C:\Heavy Logging Reports\Recorded Issues468'
dest2 = 'C:\Heavy Logging Reports\Recorded Issues478'


for f in names:
    if (f[4:].startswith("468WD")):
        shutil.copy(os.path.join(path, f), os.path.join(dest1, f))

    elif (f[4:].startswith("478WD")):
        shutil.copy(os.path.join(path, f), os.path.join(dest2, f))


print('Finished Moving Files!')