1

I need to search by part of filenames.

I have a column in my database that contains the names of products, and each product has a file for description, so I need to add this file to the same raw in my database.

I want to add a new column called description and add the contents of the file that has the same name on the column name, but the names in column and file are different, for example, the product called cs12 in the database and datasheet-cs12 or guide-cs12 or anything like this in the file

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
sherry
  • 21
  • 4
  • 3
    so, hi, "How do I search for text in files" is really something you can research a bit further on your own, and I *bet* you already did. So, where exactly are you stuck? – Marcus Müller Jul 18 '21 at 14:13
  • Does this answer your question? [How to search for a string in text files?](https://stackoverflow.com/questions/4940032/how-to-search-for-a-string-in-text-files) – Marcus Müller Jul 18 '21 at 14:13
  • Yes, I searched previously, but I need to use a "like" statement or " percent "as SQL, which means I can retrieve all files that contain the same word as the one I searched for if there are any words preceding or following this word, because I can't put the exact line, I just search by the exact word on the file names. – sherry Jul 18 '21 at 18:36
  • your question doesn't explain any of your restrictions. On the contrary, just looking for a string like yours does **not** require any "LIKE" syntax as in SQL. It's really not clear where the problem lies here. – Marcus Müller Jul 18 '21 at 18:48
  • I have a column in my database that contains the names of products, and each product has a file for description, so I need to add this file to the same raw in my database. I want to add a new column called description, and add the contain of the file that has the same name on the column name, but the names in column and file are different, for example, the product called cs12 in the database and datasheet-cs12 in the file. – sherry Jul 18 '21 at 19:03
  • **edit** your question to include **all** relevant information you can think of. The comments are not the place to state such things. I hope you see how the problem you describe in this comment is **significantly** different from the thing you're currently asking in your question – your question doesn't even mention a database. It's still not clear why you think you need a LIKE syntax to search for files (or in files? not clear.). – Marcus Müller Jul 18 '21 at 19:06
  • Thanks for your response; I was new to asking a question here and didn't know how to phrase it properly. – sherry Jul 18 '21 at 19:09
  • thank you for editing your question. Look, I've refined your title and your first sentence a bit, can you check whether it reflects your intention well? – Marcus Müller Jul 18 '21 at 19:12
  • yes exactly what i need thank you so much – sherry Jul 18 '21 at 19:19

1 Answers1

1

You will need to figure out how to

  1. get a list of files in a folder
  2. Look for a substring in each element of that list

Re 1.: https://stackoverflow.com/search?q=%5Bpython%5D+get+list+of+files+in+directory

Re 2.:

You have a logical problems. There might be multiple files that match any string. So, I don't think you can solve this fully automatically at all. What if you have two files datasheet_BAT54alternative.txt and info_BAT54A, and two rows containing the string BAT54 and BAT54A. A "BAT54" is not the same as a "BAT54A". So, you'll always have to deal with a list of candidates. If you're lucky, that list has only one entry:

def give_candidates(list_of_file_names, substring):
    return [ fname for fname in file_names if substring.lower() in fname.lower() ]
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94