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

Searching for and Moving Files Based on Part of the Filename

所以我是一家软件公司的质量控制人员,目前我正在编写一个 Python 脚本来整理超过 108,00 个错误日志。我想要做的是过滤掉来自我们以前和当前版本(分别为 4.1.468 和 4.1.478)的报告。

报告的文件名如下(Mac 报告的 MD,Windows 报告的 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!')

这应该有所帮助。

演示:

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!')