Create . txt file in PYTHON if it does not exist

Asked

Viewed 29,234 times

2

The code:

        arquivo = open(input('Nome do arquivo a ser editado:'), 'r')
        texto = arquivo.readlines()
        texto.append(input('Insira o texto:'))
        arquivo = open(input('Nome do arquivo a ser editado:'), 'w')
        arquivo.writelines(texto)
        arquivo.close()

It is a file . Existing TXT records what is written inside it. And add the text that is inserted, I found this way of doing that requires it to ask 2x the name of the file. Is there any way you don’t need to call 2x the name request ?

        File "C:\Users\Americo\Desktop\TESTE\Trabson.py", line 51, in sys_calls
        arquivo = open(input('Nome do arquivo a ser editado:'), 'r')
        FileNotFoundError: [Errno 2] No such file or directory: 'teste.txt'

And also if the file does not exist, as I do to create and put the text I wish inside warning that the file was created and that it did not exist. Another user had put in another doubt the following code snippet:

      if os.path.isfile(diretorio):
      ...
      ficheiro = open(diretorio, "w") # criamos/abrimos o ficheiro
      #.... .... ... operacoes no ficheiro
      ficheiro.close()

This is applied in this case for the confirmation of the txt file ? The excerpt worked for part of my code served for the whole part of MOVE CREATE DELETE AND RENAME some file or directory.

2 answers

5


If you do that, you don’t have to call twice.

arquivo = open(input('Nome do arquivo a ser editado:'), 'r+')
texto = arquivo.readlines()
texto.append(input('insira o valor'))
arquivo.writelines(texto)
arquivo.close()

Now, if the reported file does not exist it can be so.

try:
    nome_arquivo = input('Nome do arquivo a ser editado:')
    arquivo = open(nome_arquivo, 'r+')
except FileNotFoundError:
    arquivo = open(nome_arquivo, 'w+')
    arquivo.writelines(u'Arquivo criado pois nao existia')
#faca o que quiser
arquivo.close()
  • It was this exception that I didn’t know how to do,/ But thanks I was able to solve the problem!

2

Maybe you can use the "append" of files to resolve this issue. But, making the least change in your code, Voce could put the input in a variable, IE:

    nome_do_arquivo = input('Nome do arquivo a ser editado:')
    arquivo = open(nome_do_arquivo, 'r')
    texto = arquivo.readlines()
    texto.append(input('Insira o texto:'))
    arquivo = open(nome_do_arquivo, 'w')
    arquivo.writelines(texto)
    arquivo.close()

Or you can make the following code:

    arquivo = open(input('Nome do arquivo a ser editado:'), 'r+')
    print(arquivo.readlines()) #Consegue ler
    arquivo.write(input('Insira o texto:')+"\n") #consegue editar
    arquivo.close()

It is important to remember that the file must exist, otherwise check before and create.

  • Thanks for rationing it in this stretch I will not use it but it will serve to the next problem thankful for the help thanks for the time spent!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.