Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Permission denied writing to Windows 10

by Anonymous Monk
on Sep 14, 2016 at 15:26 UTC ( [id://1171741]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Pretty simply put, on Windows 10, when I try to open a file for writing I get 'Permission Denied' error. I have looked through this site (and others) but can't find an answer to something that I would expect would be common. So I must be doing something wrong.

use 5.010; use strict; use warnings; open FILE,"> hello.txt" or die $!; close FILE;

Permission denied at test.pl line 5.

Thoughts? Thanks!

Replies are listed 'Best First'.
Re: Permission denied writing to Windows 10
by Corion (Patriarch) on Sep 14, 2016 at 15:28 UTC

    What is your current directory? Most likely, you are simply not allowed to write to whatever directory is your current directory.

    You can find the current directory with the following Perl code:

    use Cwd; print "Current directory is: ", getcwd; open FILE, ...

      My current directory is the same directory that my perl script lives in. I am running as administrator and as far as I can tell the directory allows Full Control to admins. How can I confirm your suspicion?

        What is the full path? WinDoze got picky about writing to "\Program Files\whatever...." some time ago. Running as Admin doesn't always override that.

        Writing to C:\Public, on the other hand, is unlikely to give you problems, but whatever the issue may be, you'll be well served to create a new directory, on another (local) drive if that's available in which to put your new work. Commingling your Perl install with homework, experiments, learning-challenges and other scripts will be a hassle a few weeks or months from now.

        Just BTW, as this has already been partially addressed by way of correction, but I don't know how the open shown in your OP could work, period, given the missing close-quote, comma operator, space, open-quote sequence. Of course, sometimes I miss the memo... and I did not test your version because I expected a syntax error.

        Update: Please don't read the preceding paragraph. Pretty Please? See Rev. Canon AnomalousMonk's reply below. Watch ww flush with embarrasment! Mea Culpa.

        What happens if you type echo . > hello.txt at that same command line?


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Permission denied writing to Windows 10
by stevieb (Canon) on Sep 14, 2016 at 18:31 UTC

    I just did a test on one of my win2k8r2 boxes, and I can reproduce the permission denied error by setting the Read Only flag on the hello.txt file before running the open on it. Could this possibly be your case?

    # after setting the RO bit c:\>perl -E "open FILE, '> hello.txt' or die $!" Permission denied at -e line 1.

    Note that you should be using 3-arg open, and lexical, not bareword file handles:

    open my $fh, '>', 'hello.txt' or die $!;

      Thanks for correcting my use of open. I have changed to use the 3-arg open with lexical handles. Hello.txt as a file does not exist before running the script. The idea is that the script creates file. So if it doesn't yet exist, then it can't be RO, right? Is it possible that files are getting created as RO for some reason?

Re: Permission denied writing to Windows 10
by BrowserUk (Patriarch) on Sep 14, 2016 at 15:47 UTC

    How are you running the script? Ie. from the Explorer, or a command line, or ...


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I am running Strawberry Perl from the command line

Re: Permission denied writing to Windows 10
by Anonymous Monk on Sep 14, 2016 at 19:42 UTC

      Here is the output using Fudge()

      Error 13 Permission denied 5 Access is denied ERROR_INVALID_DATA EVENT_SYSTEM_CONTEXTHELPEND EMR_SETBRUSHORGEX EACCES EMARCH_ENC_I17_IMM5C_INST_WORD_POS_X # status($?) 0 subexit($? >>8) 0 signal($? & 127) 0 coredump($? & 128) 0 at test.pl line 33.
        you said am thinking that there is something about this specific directory tree. so i suspect too that you are running the script from within a special windows directory: for example 32 bit program cannot access the content c:\Program Files and this is due to filesystem redirection: an evil feature of such OSses: see about win file system redirection 32 64bit madness and me about filesystem redirection

        So, what is your current path, where you encounter problem? which is the minimal code that produce such error?

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Permission denied writing to Windows 10
by Pickwick (Beadle) on Sep 15, 2016 at 13:02 UTC
    Thoughts?

    Process Monitor of the Sysinternals Suite will exactly tell you which file you tried to create/access and why it failed. Just search for "ACCESS DENIED" in the monitoring results and you will find your path, which is most likely not what you think it is.

    https://technet.microsoft.com/de-de/sysinternals/processmonitor.aspx

    https://technet.microsoft.com/de-de/sysinternals/bb795533.aspx

      ou will find your path, which is most likely not what you think it is.

      That doesn't really gell with what we've discovered so far. If he can create the file from the command line using shell redirection; there should be no reason that perl running at that exact same command prompt cannot also create/write to the file.

      That places the problem firmly in the realms of Perl -- most likely there's something weird about the installation -- rather than anything to do with the OS or permissions.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
        on such OS the path is always important:

        # 64bit cmd.exe launched as Administrator (via right click runas) C:\Windows\system32>echo wrote > "c:\Program Files\_ONE.txt" C:\Windows\system32>type "c:\Program Files\_ONE.txt" wrote # Perl 32 bit C:\Windows\system32>C:\path\strP5.22-32\perl\bin\perl.exe -e "system q +q(echo WroteByPerl > $ARGV[0] ) or die $^E" "c:\Pro +gram Files\_TWO.txt" File esauriti at -e line 1. # the above error is in italian, as the OS, and means 'There are no mo +re files.' # Perl 64 bit C:\Windows\system32>C:\path\straw64\perl\bin\perl.exe -e "system qq(ec +ho WroteByPerl64bit > $ARGV[0] ) or die $^E" "c:\Progra +m Files\_TWO.txt" Accesso negato at -e line 1. # Access Denied

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        If he can create the file from the command line using shell redirection; there should be no reason that perl running at that exact same command prompt cannot also create/write to the file.

        What you were doing was guessing, Process Monitor on the other hand would have told exactly what is happening by the interesting process where, with which args and results. In cases like those guessing is a waste of time if proper tools to tell one are already available.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1171741]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found