Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • file cannot be modified or erased; likely cause is read-only directory or file

    Hi STATA users

    I´m having a rather odd problem with a seemingly random error.

    I am running a code containing several loops. Many of the loops end up with with a code like this:
    Code:
                    append using "Stats Table - Peak uni.dta"
                    duplicates drop
                    save "Stats Table - Peak uni.dta", replace
                    }
    Sometimes, and it really only is sometimes, the program stops at some random loop iteration with the following error message:
    HTML Code:
    file Stats Table - Peak uni.dta cannot be modified or erased; likely cause is
        read-only directory or file
    And sometimes the program runs to the end without this error occurring at all.

    Can anybody tell me whats going on, and perhaps point to a solution?

    I guessing it is a hardware issue since the error occurs at random. Maybe my harddisk is to slow to save the file before STATA proceeds.

    Hope you can help.


  • #2
    These kind of errors are usually due to working directory issues - try explicitly setting your working directory (cd <path>) before the loop and it should solve the problem.

    Comment


    • #3
      There is another possibility, less likely than what Jesse Wursten has pointed out. If the file you are saving is (or becomes) very large, and the loop otherwise executes quickly, it is possible that the operating system is not done writing out the last version of the file (disk output is very slow compared to computation) when it gets Stata's next request to write out the next version, and at that point the OS may tell Stata the file is inaccessible because it is still busy.

      If this is what is happening, the solution is to put a -sleep- command into the loop just after the -save- command. That will make Stata wait long enough for the file-write operations to completely finish before proceeding to the next iteration of the loop. You will need to do some trial-and-error to find out just how much sleep time is needed. I'd start with 500 milliseconds and go up from there until the loop consistently runs without failure.

      Comment


      • #4
        Thank you both very much!
        The -sleep- command was just what was needed.

        Comment


        • #5
          Deleting outreg2.ado and outreg2.hlp files from the directory and reinstalling them through ssc install outreg2 helped me.

          Comment


          • #6
            Hi all,

            I also added the sleep command to my code to

            Code:
            //9. Save matrix to spreadsheet
            mat2txt, matrix(m) saving (C:\Users\gebruiker\Desktop\Management, Economics, and Consumerism\testdocument.xlsx) sleep 10000 replace
            But I get an r(198) error saying that sleep is not allowed..

            [solved]
            Last edited by Linda WUR; 23 Jul 2020, 08:51. Reason: solved

            Comment


            • #7
              Clyde's solution to include sleep 500 worked like a charm for my case. It might be worth adding that 1) It was not a big file at all, 2) it was being executed by a fast CPU off a slowish HHD drive and 3) it definitely wasn't due to directories, as I checked these thoroughly. Thanks Cylde

              Comment


              • #8
                The same for me adding, the command

                sleep 1000

                Between the loop worked very well, thanks Clyde Schechter

                Kind regards,

                Jamel

                Comment

                Working...
                X