Advertisement
Guest User

txt edit

a guest
Oct 18th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. import os
  2.  
  3. path = os.listdir()
  4. path = [item for item in path if item.find(".txt") != -1]
  5.  
  6. cont = 1
  7. for c in path:
  8.     print([cont], c)
  9.     cont += 1
  10.  
  11. index_dir = int(input("\nSelecione o arquivo: ")) - 1
  12. # abre o txt em modo de leitura
  13. arquivo = open(path[index_dir], 'r')
  14. linhas = arquivo.read().split("\n")
  15.  
  16. c = 1
  17. for linha in linhas:
  18.     print(c,'-', linha)
  19.     c += 1
  20.  
  21. line = str(input("Número da linha + texto: ")).strip().split()
  22. # Recebe o número da linha e o texto a ser adicionado ao final dela, e sobreescreve a mesma na linha
  23. linhas[int(line[0]) - 1] = linhas[int(line[0]) - 1] + " ".join(line[1:])
  24. # reabre o txt em modo de escrita
  25. arquivo = open(path[index_dir], 'w')
  26.  
  27. while linhas[-1] == "":
  28.     if linhas[-1] == "":
  29.         linhas.pop(-1)
  30.        
  31. for linha in linhas:
  32.     if linha != " ":
  33.         linha = linha + "\n"
  34.     # Reescreve todo o txt já incluindo sua alteração
  35.     arquivo.write(linha)
  36. # Fechando o txt
  37. arquivo.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement