Function replace does not work for all cases

1

I made this script to read a TXT file, find a 20-digit sequence in the text, and rename the file with the digit sequence found. I used replace to remove all the characters that appear between numbers, but somehow it did not remove hyphens when renaming.

name_files5 = os.listdir(path_txt)

for TXT in name_files5:
    with open(path_txt + '\' + TXT, "r") as content:
        search = re.search(r'(?:\d(?:[\s,.\-\xAD_]|(?:\r)|(?:\n))*){20}', content.read())
    if search is not None:
        name5 = search.group(0)
        name5 = name5.replace("\n", "")
        name5 = name5.replace("\r", "")
        name5 = name5.replace("n", "")
        name5 = name5.replace("r", "")
        name5 = name5.replace("-", "")
        name5 = name5.replace("\", "")
        name5 = name5.replace("/", "")
        name5 = name5.replace(".", "")
        name5 = name5.replace(" ", "")
        fp = os.path.join("20_digitos", name5 + "_%d.txt")
        postfix = 0
        while os.path.exists(fp % postfix):
            postfix += 1
        os.rename(
            os.path.join(path_txt, TXT),
            fp % postfix
        )

I made other links to find other sequences for other sequences of more or less digits, using replace in the same way, including the hyphen, and worked without problems

edit: example of how the sequence appears in the text, and how it renamed the file, "_0" is just an increment to differentiate files when you already have one with the same name

As it appears in the text:

  

0001018-88.2011.5.02.0002

How did you rename:

  

0001018-8820115020002_0

    
asked by anonymous 28.11.2017 / 13:33

1 answer

-1

It will be a reference for you. You will certainly be able to implement to save the file.

import re
import os
from os import path

def search_files(prefix:'Uso: /home/user'):
    if not path.lexists(prefix):
        raise FileNotFoundError('Diretório ou arquivo não existe!')

    if path.isfile(prefix):
        return list(prefix)

    return [path.join(prefix, file) for file in os.listdir() if path.isfile(file)]

def map_numbers_in_files(prefix:'Uso: /home/user ou /home/user/file.txt'):
    absolute_path_files = search_files(prefix)

    if not absolute_path_files: 
        raise FileNotFoundError('Não existem arquivos nesse diretório.')

    pattern = re.compile(r"[\d]+")

    print('Numero'.ljust(25), 'Arquivo', sep='\t', end='\n\n')
    for path_file in absolute_path_files:
        with open(path_file) as file:
            for line in file.readlines():
                _matchs = pattern.findall(line)
                numbers = ''.join(_matchs)
                if numbers:
                    print(numbers.ljust(25), path_file, sep='\t')



map_numbers_in_files('/home/runner')

"""
Numero                      Arquivo

0                           /home/runner/_test_runner.py
1                           /home/runner/_test_runner.py
1                           /home/runner/_test_runner.py
1                           /home/runner/.bashrc
1                           /home/runner/.bashrc
1                           /home/runner/.bashrc
1000                        /home/runner/.bashrc
2000                        /home/runner/.bashrc
1                           /home/runner/.bashrc
1                           /home/runner/.bashrc
48                          /home/runner/.bashrc
6429                        /home/runner/.bashrc
1033013203300033013403300   /home/runner/.bashrc
1                           /home/runner/.bashrc
101                         /home/runner/.bashrc
01310135013601320101        /home/runner/.bashrc
1                           /home/runner/.profile
022                         /home/runner/.profile
1                           /home/runner/.bash_logout
1                           /home/runner/.bash_logout"""
    
06.12.2017 / 22:58