Rename File: 'str' object has in attribute 'group'



  • I'm having a problem that may be very trivial but I'm not getting to understand. I found in a text by regular expression a number sequence of the type XXXX/YYYYY, after that I need to rename the .txt file with the sequence found. The problem is that when trying to rename the file the python returns an error because it cannot rename a file that has a "/".

        import os
        import re
        import random
        import string
    
    # Endereco da pasta em que os documentos se encontram
    path_txt = (r'''C:\Users\mateus.ferreira\Desktop\TXT''')
    

    name_files = os.listdir(path_txt)

    for TXT in name_files:
    with open(path_txt + '\' + TXT, "r") as content:
    search = re.search(r'(([0-9]{4})(/)(([1][9][0-9][0-9])|([2][0-9][0-9][0-9])))', content.read())
    search = str(search)
    search = search.replace("/","")
    if search is not None:
    os.rename(os.path.join(path_txt, TXT),
    os.path.join("Processos3", search.group(0) + "" + str(random.randint(100, 999)) + ".txt"))

    I tried to use the replace function but ta giving some error that I'm not understanding why:

          File "C:/Users/mateus.ferreira/PycharmProjects/untitled/classificador_reclamante.py", line 50, in <module>
    os.path.join("Processos3", search.group(0) + "" + str(random.randint(100, 999)) + ".txt"))
    AttributeError: 'str' object has no attribute 'group'

    when I do a print after replace it returns:

    <_sre.SRE_Match object; span=(7449, 7458), match='82121991'>

    i.e. match='82121991', shows that to remove the replace bar worked



  • You are converting the function return search For string meaningless.

    for TXT in name_files:
        with open(path_txt + '\\' + TXT, "r") as content:
            search = re.search(r'(([0-9]{4})(/)(([1][9][0-9][0-9])|([2][0-9][0-9][0-9])))', content.read())
            search = str(search)
            search = search.replace("/","")
        if search is not None:
            os.rename(os.path.join(path_txt, TXT),
                      os.path.join("Processos3", search.group(0) + "_" + str(random.randint(100, 999)) + ".txt"))
    

    Even is working with the value of search even before checking whether it is null or not. If the intention is to remove the bars from the value sought by the regular expression, first you must capture the group and only then replace, not the opposite, as you did.

    It would be something like:

    with open(path_txt + '\\' + TXT, "r") as content:
        search = re.search(r'(([0-9]{4})(/)(([1][9][0-9][0-9])|([2][0-9][0-9][0-9])))', content.read())
        if search is not None:  # Verifica se o valor foi encontrado
            name = search.group(0)  # Captura o valor do grupo
            name = name.replace('/', '')  # Substitui a barra
            os.rename(...)  # Renomeia utilizando o valor de `name`
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2