MDAF/Sample codes/Analyse_folders_and_description.ipynb
2021-05-23 16:45:16 -04:00

71 lines
2.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "abstract-broad",
"metadata": {},
"source": [
"# Program to read through folders in a file-path and spit out description"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "similar-algeria",
"metadata": {},
"outputs": [],
"source": [
"def find_algorithm():\n",
" import os # To enable you walk through the OS directory\n",
" import re # To enable pattern recognition\n",
" \n",
" \n",
" # Take file-path and extract names of files in file-path as list data type\n",
" check_folder = input(\"Please enter the file-path of the folder: \\n\")\n",
" found_items = os.listdir(check_folder)\n",
" \n",
" \n",
" # Initialize what the pattern to be searched for should look like\n",
" pattern = \"\\W\\W\\W\"\n",
" \n",
" # Look into the individual txt files and extraxt the description \n",
" for items in range(0, len(found_items)):\n",
" print(f\"\\n({items + 1}): I found the file named: {found_items[items]}\")\n",
" inside_file = check_folder + '\\\\' + found_items[items]\n",
" \n",
" with open(inside_file, mode ='r', encoding = 'utf-8') as myfile:\n",
" print(\"Inside the file, I found this description: \\n \")\n",
" contents = myfile.readlines()\n",
" \n",
" for indexing in range(0, len(contents)):\n",
" if re.search(pattern, contents[indexing]):\n",
" print(contents[indexing+1])\n",
" break\n",
" \n",
" print(\"\\n\\n\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}