Unknown file quality for rocessing with python.
Hi, all
I have a Python script that search {oldstr} in list of files, and it works fine but for 1 file I'm having problem, my code can discover that {oldstr} in it. Even it's there 100%.
I did series of test to verify this, So looks like I need to deal with something new.
Origin for files are TFS (MS). Files were checked out, copied into c:/workdir, then processed with modern python I just learned in my class.
They all have same encoding, I'm checking it. Able to open and safe files, files looks OK in notepad++.
.......Proc file: Repl__Demo.sql: utf-8 Original from TFS
.......Proc file: Repl__Demo0.sql: utf-8 Safe As from TFS copy
.......Proc file: Repl__Demo3.sql: utf-8 Paste/Copy into new file ---OK
....... Replacements done in C:\Demo\Repl__Demo3.sql
I'm doing this pseudo logic for my script:
for root, dirs, files in os.walk(input_dir):
.....
# Match oldstr if preceded by space
pattern_match = re.compile(r"(^|\s)" + re.escape(oldstr), re.IGNORECASE)
if pattern_match.search(line):
pattern_replace = re.compile(r"(^|\s)" + re.escape(oldstr), re.IGNORECASE)
# Replace only the matched pattern, keeping the leading space or start
new_line = pattern_replace.sub(lambda m: (m.group(1) if m.group(1) else '') + newstr, line)
temp_lines.append(new_line)
changed = True
Posted Dec 7, 2025, 11:53 PM UTC