查找文件名中包含特定匹配数字或字符串的文本文件

时间:2018-02-19 04:49:32

标签: python pandas dataframe

我导入了一个带有列标题序列号Head serial#

的文件

这是一个序列号列表:

Head serial #
UG0013
UG0025
UG0043
UG0053
UG5214
UG5246
UT5324
UT0244
TH7035
TH7106
TH7212
TH7218
TH7362
C499277BT433
D499241BD221
D499227BQ004
B500438BZ921
B500425BZ933

我需要找到网络文件夹中文件名中包含这些数字的所有文本文件。请帮忙!

这是我的代码到目前为止,目前正在返回所有.txt文件,但我只想要名称中带有上述序列号的文件提前感谢!

import matplotlib.pyplot as plot
import pandas as pd
import xlrd

""" This is the master file for reading the lifetest lasers """

masterfile_location = 'C:/Users/gallachj/Documents/Lifetest_Master.xlsx'
#df = pd.read_excel(masterfile_location)



from pandas import ExcelWriter
from pandas import ExcelFile

df = pd.read_excel(masterfile_location, sheet_name='Sheet1')

#print("Column headings:")
#print(df.columns)
#print(df['Head serial #'])
sns = df['Head serial #']
headtypes = df['Head type']     
colors = df['Wavelength (nm)']
powers = df['Power rating (W)']

import fnmatch
import os
os.chdir('C:/Users/gallachj/Documents/')

for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.txt'):
# if    fnmatch.filter(sns, '*.txt')
(print(file))`

1 个答案:

答案 0 :(得分:0)

基于来自 Noufal Ibrahim this answer,你可以尝试这样的事情:

import os
def find_filenames(d, s): 
    try:
        files = os.listdir(d)
    except PermissionError:  # network drives usually have a lot inaccessible folders 
        return []

    matched_files = []
    for f in files:  # loop over elements in folder
        full_name = os.path.join(d, f) # get full relative name
        if os.path.isdir(full_name): # recursive call
            matched_files += find_filenames(full_name, s)
        elif os.path.isfile(full_name): 
            if any(serial in f for serial in s):  # check the filename
                matched_files.append(os.path.realpath(f)) # Remember the matched file
    return matched_files # Return a list of matched files

find_filenames(r'\\40i2039p-0\d\_Mer_', ['search', 'words'])