Error running python script via python 2 terminal: "Ioerror: [Errno 2] No such file or directory:"

Asked

Viewed 637 times

0

The program should create a mes/dia.dat file, but nothing is created and this error is returned to me, where we can see that the file is not being created with the file.open() command. Follow the code of the program. I found in the stack something about the program not recognizing the directory in which I am running the program, however, even after adding "import os", there are no results.

import os

ch=1
while ch != 0:
    print("###  Script rodando. Bem-vindo a inserção de dados.  ####\n\n")
    m = int(input('Insira o mes:'))
    d = int(input('Insira o dia:'))

    name = '{}/{}'.format(m,d)
    curpath = os.path.abspath(os.curdir)

    #abrir o mes/dia.dat aqui
    file = open("%s.txt" %name, "w+")

    qt = int(input('Insira a quantidade de alunos presentes.'))
    for i in range(0,qt):
            nome = raw_input('Insira o nome do aluno:')
            course = raw_input('Insira o curso:')
            data = '{} {}'.format(nome,course)
            file.write(data)

    file.close()
    ch = int(input('Fim das opções.\nInsira 0 para fechar ou 1 para inserir dados em outro arquivo:'))
  • First of all in the file, put print "programa iniciado com sucesso" and post the return.

  • Are you using python2 or 3?

  • I am using python2

  • He may be searching in the root directory as the answer below says, or actually understanding the "/" that separates month and day as one directory within another

1 answer

1

The error is on the line you set the file name, when you pass the / between the month and day this way name = '{}/{}'.format(m,d) he understands that he should fetch a folder in the root directory with the number of the month to record a file with the name of the day, which would look like this c:/seu/diretorio/raiz/mes/dia.txt. With this it explodes this error by not finding the directory or file.

My suggestion would be to define the file name without the /, can be a _ or - or any other character

for example:

name = '{}-{}'.format(m,d)

Browser other questions tagged

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