Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- path = os.listdir()
- path = [item for item in path if item.find(".txt") != -1]
- cont = 1
- for c in path:
- print([cont], c)
- cont += 1
- index_dir = int(input("\nSelecione o arquivo: ")) - 1
- # abre o txt em modo de leitura
- arquivo = open(path[index_dir], 'r')
- linhas = arquivo.read().split("\n")
- c = 1
- for linha in linhas:
- print(c,'-', linha)
- c += 1
- line = str(input("Número da linha + texto: ")).strip().split()
- # Recebe o número da linha e o texto a ser adicionado ao final dela, e sobreescreve a mesma na linha
- linhas[int(line[0]) - 1] = linhas[int(line[0]) - 1] + " ".join(line[1:])
- # reabre o txt em modo de escrita
- arquivo = open(path[index_dir], 'w')
- while linhas[-1] == "":
- if linhas[-1] == "":
- linhas.pop(-1)
- for linha in linhas:
- if linha != " ":
- linha = linha + "\n"
- # Reescreve todo o txt já incluindo sua alteração
- arquivo.write(linha)
- # Fechando o txt
- arquivo.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement