[Python] Why do I get Python IOError: [Errno 13] Permission denied on writing to a file?

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I cannot figure out what I am doing wrong. I am running some tests and writing the results to a file. The portion of the code which writes to a file is the following (in a class called Tester):

    @staticmethod
    def printHeader(resultsFileName):
    if not os.path.isfile(resultsFileName):
    # The file does not exist, thus
    # we need to print the header

    # Opens the results file
    with open(resultsFileName,"a") as file:
    # Prints the header
    file.write("A long header")


    @staticmethod
    def printResults(resultsFileName,otherArguments):

    # Prints the header if it does not exist
    Tester.printHeader(resultsFileName)

    # Prints the results
    with open(resultsFileName,"a") as file:

    file.write(otherArguments)


    Sometimes I get this error:

    Traceback (most recent call last):
    File "main.py", line 74, in <module>
    File "tester.py", line 88, in methodOne
    File "tester.py", line 441, in printResults
    File "tester.py", line 428, in printHeader
    IOError: [Errno 13] Permission denied: 'results.txt'


    while other times it runs smoothly. I cannot figure out where the problem is. Any ideas?

    NOTE1: I have rwxpermission on the directory where the file is written. NOTE2: The error happens after several lines of results have already been written. Thus, it happens when the code is checking whether the header should be printed or not (but it is not supposed to print it, since the file exists).

    UPDATE 1:

    As suggested, I have changed my code to avoid opening and closing the file multiple times. Now it writes everything in one shot. This is the updated code:

    @staticmethod
    def printResults(resultsFileName,otherArguments):

    # Prints the header if it does not exist
    if not os.path.exists(resultsFileName):

    # The file does not exist, thus
    # we need to print the header

    # Opens the results file
    # HERE IS WHERE ERRNO 13 HAPPENS
    # STRANGELY, THE FILE DOES EXIST
    # AS SEVERAL LINES OF RESULTS
    # HAVE ALREADY BEEN WRITTEN
    with open(resultsFileName,"w") as file:

    # Prints the header
    file.write("A suitable header")
    # Prints the results
    file.write(otherArguments)
    else:

    # Prints the results
    with open(resultsFileName,"a") as file:
    file.write(otherArguments)


    It seems that os.path.exists() at some point returns FALSEeven if the file does exist. Probably, there is something revoking me permission to write (perhaps the file is not properly closed after writing?).

    The explanation of os.path.exists() says that:


    On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
    ​

    UPDATE 2

    I have changed my code to the following, to avoid os.path.isfile():

    # Opens the results file
    with open(resultsFileName,"a") as file:
    if file.tell() == 0:
    # Prints the header
    file.write("Header")
    # Prints the results
    file.write(otherArguments)
    file.close()
    else:

    # Prints the results
    file.write(otherArguments)
    file.close()


    Nevertheless, ERRNO 13 happens at with open(resultsFileName,"a") as file:. I have rwpermissions both on the folder and on the file, on which several lines are written before the error happens. The OS is Linux.

    Continue reading...

Compartilhe esta Página