简体   繁体   中英

Creating a match between string and file name in lists

Let's say I have a list containing strings of the form "{N} word" (without quotation marks), where N is some integer and word is some string. In some folder D:\\path\\folder , I have plenty of files with names of the form " {N}name.filetype ". With an input of the aforementioned list (elements being "{N}"), how would I get an output of a list, where every element is of the following form: " {N} words D:\\path\\folder\\{N}name.filetype "?
For example...

InputList = [{75} Hello, {823} World, ...]  

OutputList = [{75} Hello D:\path\folder\{75}Stuff.docx, {823} World D:\path\folder\{823}Things.docx, ...]  

if folder at D:\\path\\folder contains, among other files, {75}Stuff.docx and {823}Things.docx .

To generalize, my question is fundamentally:
How do I get python to read a folder and take the absolute path of any file that contains only some part of every element in the list (in this case, we look for {N} in the file names and disregard the word) and add that path to every corresponding element in the list to make the output list?

I understand this is a bit of a long question that combines a couple concepts so I highly thank anyone willing to help in advance!

The important step is to convert your InputList to a dict of {number: word} - this makes it significantly easier to work with. After that it's just a matter of looping through the files in the folder, extracting the number from their name and looking them up in the dict:

InputList = ['{75} Hello', '{823} World']
folder_path= r'D:\path\folder'

# define a function to extract the number between curly braces
def extract_number(text):
    return text[1:text.find('}')]

from pathlib import Path

# convert the InputList to a dict for easy and efficient lookup
words= {extract_number(name):name for name in InputList}

OutputList= []
# iterate through the folder to find matching files
for path in Path(folder_path).iterdir():
    # extract the file name from the path, e.g. "{75}Stuff.docx"
    name= path.name

    # extract the number from the file name and find the matching word
    number= extract_number(name)
    try:
        word= words[number]
    except KeyError: # if no matching word exists, skip this file
        continue

    # put the path and the word together and add them to the output list
    path= '{} {}'.format(word, path)
    OutputList.append(path)

print(OutputList)
# output: ['{75} Hello D:\\path\\folder\\{75}Stuff.docx', '{823} World D:\\path\\folder\\{823}Things.docx']

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