簡體   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