简体   繁体   中英

Searching for and Moving Files Based on Part of the Filename

So i'm a QC for a software company and i'm currently stuck on a Python script i'm writing to sort through over 108,00 error logs. What i'm trying to do is filter out the reports that come from our previous and current release versions (4.1.468 and 4.1.478 respectively).

The reports appear with filenames like this(with MD for Mac Reports, and WD for Windows Reports):

4.1.468MD.OutOfBoundsException.RaiseOutOfBoundsException.1

The first part of my script works, looking for and creating folders (if they don't already exist) to copy the selected reports to. But the actual copying of the reports never happens.

Any advice or pointers you can offer would be greatly received.

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

This should help.

Demo:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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