繁体   English   中英

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

[英]Searching for and Moving Files Based on Part of the Filename

因此,我是一家软件公司的质量控制人员,目前正在使用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!')

这应该有所帮助。

演示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM