File size: 18,829 Bytes
00b2f69 8ccf3ec 00b2f69 40c0a86 00b2f69 8ccf3ec 00b2f69 8ccf3ec d87b607 8ccf3ec d87b607 8ccf3ec 00b2f69 d87b607 00b2f69 8ccf3ec c513f21 8ccf3ec c513f21 8ccf3ec c513f21 fc2322a c513f21 8ccf3ec c513f21 8ccf3ec c513f21 fc2322a c513f21 fc2322a c513f21 fc2322a c513f21 8ccf3ec c513f21 8ccf3ec c513f21 fc2322a 00b2f69 40c0a86 00b2f69 40c0a86 d87b607 00b2f69 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Escape parentheses\n",
"----\n",
"\n",
"This script is designed to process a directory and its subdirectories for text files. For each text file, it reads the content and checks for any unescaped parentheses. If it finds any, it escapes them and writes the modified content back to the file. If any modifications are made, it prints a warning message along with the original and modified content. The directory to be processed is specified by the `directory_path` variable. In this case, it is set to `C:\\Users\\kade\\Desktop\\training_dir_staging`. The script starts processing from this directory. \n",
"\n",
"The **`escape_parentheses(file_path)`** function takes a file path as an argument, reads the file content, and checks for unescaped parentheses. It uses regular expressions to find unescaped parentheses and escapes them. If the content is modified, it prints a warning message along with the original and modified content. Then, it writes the modified content back to the file.\n",
"\n",
"The **`process_directory(directory)`** function takes a directory path as an argument and processes it recursively. It uses the `os.walk()` function to iterate over the directory, its subdirectories, and files. For each text file (files ending with `.txt`), it calls the `escape_parentheses(file_path)` function to escape unescaped parentheses. It also processes each subdirectory in the same way. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import re\n",
"\n",
"def escape_parentheses(file_path):\n",
" with open(file_path, \"r\") as file:\n",
" content = file.read()\n",
" original_content = content\n",
"\n",
" # Escape unescaped opening parentheses\n",
" content = re.sub(r\"(?<!\\\\)(\\\\*)(\\\\()\", r\"\\1\\\\\\2\", content)\n",
" # Escape unescaped closing parentheses\n",
" content = re.sub(r\"(?<!\\\\)(\\\\*)(\\\\))\", r\"\\1\\\\\\2\", content)\n",
"\n",
" if content != original_content:\n",
" print(f\"Warning: File '{file_path}' was modified.\")\n",
" print(f\"Original: {original_content}\")\n",
" print(f\"Modified: {content}\")\n",
"\n",
" with open(file_path, \"w\") as file:\n",
" file.write(content)\n",
"\n",
"def process_directory(directory):\n",
" for root, dirs, files in os.walk(directory):\n",
" for file in files:\n",
" if file.endswith(\".txt\"):\n",
" file_path = os.path.join(root, file)\n",
" escape_parentheses(file_path)\n",
" for dir in dirs:\n",
" process_directory(os.path.join(root, dir))\n",
"\n",
"directory_path = r\"E:\\training_dir\"\n",
"# directory_path = r\"C:\\Users\\kade\\Desktop\\training_dir_staging\"\n",
"process_directory(directory_path)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Needs Testing\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "error",
"evalue": "missing ), unterminated subpattern at position 26",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31merror\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[8], line 31\u001b[0m\n\u001b[0;32m 27\u001b[0m escape_parentheses(\u001b[38;5;28mstr\u001b[39m(file_path))\n\u001b[0;32m 30\u001b[0m directory_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mE:\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mtraining_dir\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m---> 31\u001b[0m \u001b[43mprocess_directory\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdirectory_path\u001b[49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[1;32mIn[8], line 27\u001b[0m, in \u001b[0;36mprocess_directory\u001b[1;34m(directory)\u001b[0m\n\u001b[0;32m 25\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mprocess_directory\u001b[39m(directory):\n\u001b[0;32m 26\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m file_path \u001b[38;5;129;01min\u001b[39;00m Path(directory)\u001b[38;5;241m.\u001b[39mrglob(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m*.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m---> 27\u001b[0m \u001b[43mescape_parentheses\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mfile_path\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[1;32mIn[8], line 11\u001b[0m, in \u001b[0;36mescape_parentheses\u001b[1;34m(file_path)\u001b[0m\n\u001b[0;32m 8\u001b[0m original_content \u001b[38;5;241m=\u001b[39m content\n\u001b[0;32m 10\u001b[0m \u001b[38;5;66;03m# Escape unescaped opening parentheses\u001b[39;00m\n\u001b[1;32m---> 11\u001b[0m content \u001b[38;5;241m=\u001b[39m \u001b[43mre\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msub\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m(?<!\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43m)(?<!\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43m)(\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43m\\\u001b[39;49m\u001b[38;5;124;43m*)\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43m(\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43m1\u001b[39;49m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;130;43;01m\\\\\u001b[39;49;00m\u001b[38;5;124;43m(\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontent\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 13\u001b[0m \u001b[38;5;66;03m# Escape unescaped closing parentheses\u001b[39;00m\n\u001b[0;32m 14\u001b[0m content \u001b[38;5;241m=\u001b[39m re\u001b[38;5;241m.\u001b[39msub(\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m(?<!\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124m)(?<!\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124m)(\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124m*)\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124m1\u001b[39m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;130;01m\\\\\u001b[39;00m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m, content)\n",
"File \u001b[1;32mC:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\\Lib\\re\\__init__.py:186\u001b[0m, in \u001b[0;36msub\u001b[1;34m(pattern, repl, string, count, flags)\u001b[0m\n\u001b[0;32m 179\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msub\u001b[39m(pattern, repl, string, count\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m, flags\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m):\n\u001b[0;32m 180\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Return the string obtained by replacing the leftmost\u001b[39;00m\n\u001b[0;32m 181\u001b[0m \u001b[38;5;124;03m non-overlapping occurrences of the pattern in string by the\u001b[39;00m\n\u001b[0;32m 182\u001b[0m \u001b[38;5;124;03m replacement repl. repl can be either a string or a callable;\u001b[39;00m\n\u001b[0;32m 183\u001b[0m \u001b[38;5;124;03m if a string, backslash escapes in it are processed. If it is\u001b[39;00m\n\u001b[0;32m 184\u001b[0m \u001b[38;5;124;03m a callable, it's passed the Match object and must return\u001b[39;00m\n\u001b[0;32m 185\u001b[0m \u001b[38;5;124;03m a replacement string to be used.\"\"\"\u001b[39;00m\n\u001b[1;32m--> 186\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_compile\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpattern\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mflags\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39msub(repl, string, count)\n",
"File \u001b[1;32mC:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\\Lib\\re\\__init__.py:307\u001b[0m, in \u001b[0;36m_compile\u001b[1;34m(pattern, flags)\u001b[0m\n\u001b[0;32m 301\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mwarnings\u001b[39;00m\n\u001b[0;32m 302\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe re.TEMPLATE/re.T flag is deprecated \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 303\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is an undocumented flag \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 304\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwithout an obvious purpose. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 305\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDon\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt use it.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 306\u001b[0m \u001b[38;5;167;01mDeprecationWarning\u001b[39;00m)\n\u001b[1;32m--> 307\u001b[0m p \u001b[38;5;241m=\u001b[39m \u001b[43m_compiler\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompile\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpattern\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mflags\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 308\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m flags \u001b[38;5;241m&\u001b[39m DEBUG:\n\u001b[0;32m 309\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m p\n",
"File \u001b[1;32mC:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\\Lib\\re\\_compiler.py:745\u001b[0m, in \u001b[0;36mcompile\u001b[1;34m(p, flags)\u001b[0m\n\u001b[0;32m 743\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m isstring(p):\n\u001b[0;32m 744\u001b[0m pattern \u001b[38;5;241m=\u001b[39m p\n\u001b[1;32m--> 745\u001b[0m p \u001b[38;5;241m=\u001b[39m \u001b[43m_parser\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mparse\u001b[49m\u001b[43m(\u001b[49m\u001b[43mp\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mflags\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 746\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 747\u001b[0m pattern \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n",
"File \u001b[1;32mC:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\\Lib\\re\\_parser.py:979\u001b[0m, in \u001b[0;36mparse\u001b[1;34m(str, flags, state)\u001b[0m\n\u001b[0;32m 976\u001b[0m state\u001b[38;5;241m.\u001b[39mflags \u001b[38;5;241m=\u001b[39m flags\n\u001b[0;32m 977\u001b[0m state\u001b[38;5;241m.\u001b[39mstr \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m\n\u001b[1;32m--> 979\u001b[0m p \u001b[38;5;241m=\u001b[39m \u001b[43m_parse_sub\u001b[49m\u001b[43m(\u001b[49m\u001b[43msource\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstate\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mflags\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m&\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mSRE_FLAG_VERBOSE\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 980\u001b[0m p\u001b[38;5;241m.\u001b[39mstate\u001b[38;5;241m.\u001b[39mflags \u001b[38;5;241m=\u001b[39m fix_flags(\u001b[38;5;28mstr\u001b[39m, p\u001b[38;5;241m.\u001b[39mstate\u001b[38;5;241m.\u001b[39mflags)\n\u001b[0;32m 982\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m source\u001b[38;5;241m.\u001b[39mnext \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
"File \u001b[1;32mC:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\\Lib\\re\\_parser.py:460\u001b[0m, in \u001b[0;36m_parse_sub\u001b[1;34m(source, state, verbose, nested)\u001b[0m\n\u001b[0;32m 458\u001b[0m start \u001b[38;5;241m=\u001b[39m source\u001b[38;5;241m.\u001b[39mtell()\n\u001b[0;32m 459\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m--> 460\u001b[0m itemsappend(\u001b[43m_parse\u001b[49m\u001b[43m(\u001b[49m\u001b[43msource\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstate\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mverbose\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnested\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 461\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mnested\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mand\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mitems\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[0;32m 462\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m sourcematch(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m|\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[0;32m 463\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n",
"File \u001b[1;32mC:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\\Lib\\re\\_parser.py:864\u001b[0m, in \u001b[0;36m_parse\u001b[1;34m(source, state, verbose, nested, first)\u001b[0m\n\u001b[0;32m 862\u001b[0m p \u001b[38;5;241m=\u001b[39m _parse_sub(source, state, sub_verbose, nested \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m 863\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m source\u001b[38;5;241m.\u001b[39mmatch(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m--> 864\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m source\u001b[38;5;241m.\u001b[39merror(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmissing ), unterminated subpattern\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m 865\u001b[0m source\u001b[38;5;241m.\u001b[39mtell() \u001b[38;5;241m-\u001b[39m start)\n\u001b[0;32m 866\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m group \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 867\u001b[0m state\u001b[38;5;241m.\u001b[39mclosegroup(group, p)\n",
"\u001b[1;31merror\u001b[0m: missing ), unterminated subpattern at position 26"
]
}
],
"source": [
"import os\n",
"import re\n",
"\n",
"\n",
"def escape_parentheses(file_path):\n",
" with open(file_path, \"r\") as file:\n",
" content = file.read()\n",
" original_content = content\n",
"\n",
" # Escape unescaped opening parentheses\n",
" content = re.sub(r\"(?<!\\\\)(\\\\*)(\\()\", r\"\\1\\\\\\2\", content)\n",
" # Escape unescaped closing parentheses\n",
" content = re.sub(r\"(?<!\\\\)(\\\\*)(\\))\", r\"\\1\\\\\\2\", content)\n",
"\n",
" if content != original_content:\n",
" print(f\"Warning: File '{file_path}' was modified.\")\n",
" print(f\"Original: {original_content}\")\n",
" print(f\"Modified: {content}\")\n",
"\n",
" with open(file_path, \"w\") as file:\n",
" file.write(content)\n",
"\n",
"\n",
"def process_directory(directory):\n",
" for root, dirs, files in os.walk(directory):\n",
" for file in files:\n",
" if file.endswith(\".txt\"):\n",
" file_path = os.path.join(root, file)\n",
" escape_parentheses(file_path)\n",
" for dir in dirs:\n",
" process_directory(os.path.join(root, dir))\n",
"\n",
"\n",
"directory_path = r\"E:\\training_dir\"\n",
"# directory_path = r\"C:\\Users\\kade\\Desktop\\training_dir_staging\"\n",
"process_directory(directory_path)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import re\n",
"import glob\n",
"\n",
"\n",
"def escape_parentheses(file_path):\n",
" with open(file_path, \"r\") as file:\n",
" content = file.read()\n",
" original_content = content\n",
"\n",
" # Replace any (not preceded by \\) with \\(\n",
" content = re.sub(r\"(?<!\\\\)(\\()\", r\"\\\\\\1\", content)\n",
"\n",
" # Replace any )not preceded by \\) with \\)\n",
" content = re.sub(r\"(?<!\\\\)(\\))\", r\"\\\\\\1\", content)\n",
"\n",
" if content != original_content:\n",
" print(f\"Warning: File '{file_path}' was modified.\")\n",
" print(f\"Original: {original_content}\")\n",
" print(f\"Modified: {content}\")\n",
"\n",
" with open(file_path, \"w\") as file:\n",
" file.write(content)\n",
"\n",
"\n",
"def process_directory(directory):\n",
" for file_path in glob.glob(directory + \"/**/*.txt\", recursive=True):\n",
" escape_parentheses(file_path)\n",
"\n",
"\n",
"# directory_path = r\"E:\\training_dir\"\n",
"directory_path = r\"C:\\Users\\kade\\Desktop\\training_dir_staging\"\n",
"process_directory(directory_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|