简体   繁体   中英

How do I match a list of strings to a list of of filenames so I can save those files into one master file?

I have a list of barcodes. I want to read and append files from a folder that match the barcode, but of course the barcodes are not a 1-to-1 match.

Example of the Barcode is 07002991H3 and an Example of the File Name is 07002991H3001 .

I am able to match the barcodes with a trimmed file name, but the file will is not able to read in

import pandas as pd
import glob
import os

with open('BarcodeList.txt','r') as WaferList:

    lines = WaferList.read().splitlines()

    FileList = os.listdir('//FolderThatContainsFiles')

    df = []
    for file in FileList:
        for afile in lines:

            if afile == file.split("_")[0][0:10]:
                df = pd.read_csv(file)
  ### The "df" step above does not work ###

                print('success')
   ### The success part works  ####

I expect the df step above to read the csv of matched file, but instead I receive this message:

FileNotFoundError: File b'07001382A7044_summary.csv' does not exist

You need to give pandas the file path as well as the file name; try

df = pd.read_csv(os.path.join('//FolderThatContainsFiles', file))

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