os.open doesn't create files with the given permission mode












2















When trying to open a file using os.open with the mode 777 (meaning allow everything) -



os.open("/tmp/lol", flags=(os.O_CREAT), mode=0o777)


It creates the file without the write permission like this -



-rwxrwxr-x  1 cybellum cybellum       0 Nov 20 09:38 lol*


When trying to use chmod("/tmp/lol", 0o777), the file gains the right permissions:



-rwxrwxrwx  1 cybellum cybellum       0 Nov 20 09:38 lol*


Why doesn't os.open work as expected?

And is there a way to create a file with the 777 mode (and if the file exists it will just change the permissions.. (Because I tried pathlib.Path.touch))?










share|improve this question





























    2















    When trying to open a file using os.open with the mode 777 (meaning allow everything) -



    os.open("/tmp/lol", flags=(os.O_CREAT), mode=0o777)


    It creates the file without the write permission like this -



    -rwxrwxr-x  1 cybellum cybellum       0 Nov 20 09:38 lol*


    When trying to use chmod("/tmp/lol", 0o777), the file gains the right permissions:



    -rwxrwxrwx  1 cybellum cybellum       0 Nov 20 09:38 lol*


    Why doesn't os.open work as expected?

    And is there a way to create a file with the 777 mode (and if the file exists it will just change the permissions.. (Because I tried pathlib.Path.touch))?










    share|improve this question



























      2












      2








      2








      When trying to open a file using os.open with the mode 777 (meaning allow everything) -



      os.open("/tmp/lol", flags=(os.O_CREAT), mode=0o777)


      It creates the file without the write permission like this -



      -rwxrwxr-x  1 cybellum cybellum       0 Nov 20 09:38 lol*


      When trying to use chmod("/tmp/lol", 0o777), the file gains the right permissions:



      -rwxrwxrwx  1 cybellum cybellum       0 Nov 20 09:38 lol*


      Why doesn't os.open work as expected?

      And is there a way to create a file with the 777 mode (and if the file exists it will just change the permissions.. (Because I tried pathlib.Path.touch))?










      share|improve this question
















      When trying to open a file using os.open with the mode 777 (meaning allow everything) -



      os.open("/tmp/lol", flags=(os.O_CREAT), mode=0o777)


      It creates the file without the write permission like this -



      -rwxrwxr-x  1 cybellum cybellum       0 Nov 20 09:38 lol*


      When trying to use chmod("/tmp/lol", 0o777), the file gains the right permissions:



      -rwxrwxrwx  1 cybellum cybellum       0 Nov 20 09:38 lol*


      Why doesn't os.open work as expected?

      And is there a way to create a file with the 777 mode (and if the file exists it will just change the permissions.. (Because I tried pathlib.Path.touch))?







      python linux chmod pathlib






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 10:05









      martineau

      68k1090183




      68k1090183










      asked Nov 20 '18 at 10:00









      DrxxdDrxxd

      665415




      665415
























          1 Answer
          1






          active

          oldest

          votes


















          1














          When you create a file with open, the permissions specified in the open call are modified by your umask setting. The umask defines bits that are "masked out". On my system, it looks like my current umask is 0002:



          $ umask
          0002


          This means that when I run code like yours:



          import os
          os.open('testfile', flags=(os.O_CREAT), mode=0o777)


          I'll get the following behavior:



          $ python filetest
          $ ls -l testfile
          -rwxrwxr-x. 1 lars lars 0 Nov 20 07:47 testfile


          I can set the umask to different values to control the permissions
          applied by default:



          $ umask 022
          $ python filetest
          $ ls -l testfile
          -rwxr-xr-x. 1 lars lars 0 Nov 20 07:49 testfile


          Or:



          $ umask 077
          $ python filetest.py
          $ ls -l testfile
          -rwx------. 1 lars lars 0 Nov 20 07:50 testfile


          Read more here.






          share|improve this answer
























          • Note that Python includes os.umask for manipulating the umask.

            – ShadowRanger
            Nov 20 '18 at 12:53






          • 1





            Sure, although I would argue that in most cases you want to let the user manipulate their umask external to your code. If someone has explicitly set their umask to avoid creating world-readable documents, it can be surprising when something goes behind their back to make the change anyway.

            – larsks
            Nov 20 '18 at 12:57











          • Interesting! But what if you don't want to open all of the files using the set umask and use a specific file creation functionality that will be permissionless?

            – Drxxd
            Nov 20 '18 at 13:00











          • You can set umask to 0, of course, either external to your code or, as @ShadowRanger has suggested, using os.umask.

            – larsks
            Nov 20 '18 at 13:06











          • @larsks: Agreed that usually the user should be in charge of their umask, not the program.

            – ShadowRanger
            Nov 20 '18 at 13:35











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390466%2fos-open-doesnt-create-files-with-the-given-permission-mode%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          When you create a file with open, the permissions specified in the open call are modified by your umask setting. The umask defines bits that are "masked out". On my system, it looks like my current umask is 0002:



          $ umask
          0002


          This means that when I run code like yours:



          import os
          os.open('testfile', flags=(os.O_CREAT), mode=0o777)


          I'll get the following behavior:



          $ python filetest
          $ ls -l testfile
          -rwxrwxr-x. 1 lars lars 0 Nov 20 07:47 testfile


          I can set the umask to different values to control the permissions
          applied by default:



          $ umask 022
          $ python filetest
          $ ls -l testfile
          -rwxr-xr-x. 1 lars lars 0 Nov 20 07:49 testfile


          Or:



          $ umask 077
          $ python filetest.py
          $ ls -l testfile
          -rwx------. 1 lars lars 0 Nov 20 07:50 testfile


          Read more here.






          share|improve this answer
























          • Note that Python includes os.umask for manipulating the umask.

            – ShadowRanger
            Nov 20 '18 at 12:53






          • 1





            Sure, although I would argue that in most cases you want to let the user manipulate their umask external to your code. If someone has explicitly set their umask to avoid creating world-readable documents, it can be surprising when something goes behind their back to make the change anyway.

            – larsks
            Nov 20 '18 at 12:57











          • Interesting! But what if you don't want to open all of the files using the set umask and use a specific file creation functionality that will be permissionless?

            – Drxxd
            Nov 20 '18 at 13:00











          • You can set umask to 0, of course, either external to your code or, as @ShadowRanger has suggested, using os.umask.

            – larsks
            Nov 20 '18 at 13:06











          • @larsks: Agreed that usually the user should be in charge of their umask, not the program.

            – ShadowRanger
            Nov 20 '18 at 13:35
















          1














          When you create a file with open, the permissions specified in the open call are modified by your umask setting. The umask defines bits that are "masked out". On my system, it looks like my current umask is 0002:



          $ umask
          0002


          This means that when I run code like yours:



          import os
          os.open('testfile', flags=(os.O_CREAT), mode=0o777)


          I'll get the following behavior:



          $ python filetest
          $ ls -l testfile
          -rwxrwxr-x. 1 lars lars 0 Nov 20 07:47 testfile


          I can set the umask to different values to control the permissions
          applied by default:



          $ umask 022
          $ python filetest
          $ ls -l testfile
          -rwxr-xr-x. 1 lars lars 0 Nov 20 07:49 testfile


          Or:



          $ umask 077
          $ python filetest.py
          $ ls -l testfile
          -rwx------. 1 lars lars 0 Nov 20 07:50 testfile


          Read more here.






          share|improve this answer
























          • Note that Python includes os.umask for manipulating the umask.

            – ShadowRanger
            Nov 20 '18 at 12:53






          • 1





            Sure, although I would argue that in most cases you want to let the user manipulate their umask external to your code. If someone has explicitly set their umask to avoid creating world-readable documents, it can be surprising when something goes behind their back to make the change anyway.

            – larsks
            Nov 20 '18 at 12:57











          • Interesting! But what if you don't want to open all of the files using the set umask and use a specific file creation functionality that will be permissionless?

            – Drxxd
            Nov 20 '18 at 13:00











          • You can set umask to 0, of course, either external to your code or, as @ShadowRanger has suggested, using os.umask.

            – larsks
            Nov 20 '18 at 13:06











          • @larsks: Agreed that usually the user should be in charge of their umask, not the program.

            – ShadowRanger
            Nov 20 '18 at 13:35














          1












          1








          1







          When you create a file with open, the permissions specified in the open call are modified by your umask setting. The umask defines bits that are "masked out". On my system, it looks like my current umask is 0002:



          $ umask
          0002


          This means that when I run code like yours:



          import os
          os.open('testfile', flags=(os.O_CREAT), mode=0o777)


          I'll get the following behavior:



          $ python filetest
          $ ls -l testfile
          -rwxrwxr-x. 1 lars lars 0 Nov 20 07:47 testfile


          I can set the umask to different values to control the permissions
          applied by default:



          $ umask 022
          $ python filetest
          $ ls -l testfile
          -rwxr-xr-x. 1 lars lars 0 Nov 20 07:49 testfile


          Or:



          $ umask 077
          $ python filetest.py
          $ ls -l testfile
          -rwx------. 1 lars lars 0 Nov 20 07:50 testfile


          Read more here.






          share|improve this answer













          When you create a file with open, the permissions specified in the open call are modified by your umask setting. The umask defines bits that are "masked out". On my system, it looks like my current umask is 0002:



          $ umask
          0002


          This means that when I run code like yours:



          import os
          os.open('testfile', flags=(os.O_CREAT), mode=0o777)


          I'll get the following behavior:



          $ python filetest
          $ ls -l testfile
          -rwxrwxr-x. 1 lars lars 0 Nov 20 07:47 testfile


          I can set the umask to different values to control the permissions
          applied by default:



          $ umask 022
          $ python filetest
          $ ls -l testfile
          -rwxr-xr-x. 1 lars lars 0 Nov 20 07:49 testfile


          Or:



          $ umask 077
          $ python filetest.py
          $ ls -l testfile
          -rwx------. 1 lars lars 0 Nov 20 07:50 testfile


          Read more here.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 12:50









          larskslarsks

          118k19197205




          118k19197205













          • Note that Python includes os.umask for manipulating the umask.

            – ShadowRanger
            Nov 20 '18 at 12:53






          • 1





            Sure, although I would argue that in most cases you want to let the user manipulate their umask external to your code. If someone has explicitly set their umask to avoid creating world-readable documents, it can be surprising when something goes behind their back to make the change anyway.

            – larsks
            Nov 20 '18 at 12:57











          • Interesting! But what if you don't want to open all of the files using the set umask and use a specific file creation functionality that will be permissionless?

            – Drxxd
            Nov 20 '18 at 13:00











          • You can set umask to 0, of course, either external to your code or, as @ShadowRanger has suggested, using os.umask.

            – larsks
            Nov 20 '18 at 13:06











          • @larsks: Agreed that usually the user should be in charge of their umask, not the program.

            – ShadowRanger
            Nov 20 '18 at 13:35



















          • Note that Python includes os.umask for manipulating the umask.

            – ShadowRanger
            Nov 20 '18 at 12:53






          • 1





            Sure, although I would argue that in most cases you want to let the user manipulate their umask external to your code. If someone has explicitly set their umask to avoid creating world-readable documents, it can be surprising when something goes behind their back to make the change anyway.

            – larsks
            Nov 20 '18 at 12:57











          • Interesting! But what if you don't want to open all of the files using the set umask and use a specific file creation functionality that will be permissionless?

            – Drxxd
            Nov 20 '18 at 13:00











          • You can set umask to 0, of course, either external to your code or, as @ShadowRanger has suggested, using os.umask.

            – larsks
            Nov 20 '18 at 13:06











          • @larsks: Agreed that usually the user should be in charge of their umask, not the program.

            – ShadowRanger
            Nov 20 '18 at 13:35

















          Note that Python includes os.umask for manipulating the umask.

          – ShadowRanger
          Nov 20 '18 at 12:53





          Note that Python includes os.umask for manipulating the umask.

          – ShadowRanger
          Nov 20 '18 at 12:53




          1




          1





          Sure, although I would argue that in most cases you want to let the user manipulate their umask external to your code. If someone has explicitly set their umask to avoid creating world-readable documents, it can be surprising when something goes behind their back to make the change anyway.

          – larsks
          Nov 20 '18 at 12:57





          Sure, although I would argue that in most cases you want to let the user manipulate their umask external to your code. If someone has explicitly set their umask to avoid creating world-readable documents, it can be surprising when something goes behind their back to make the change anyway.

          – larsks
          Nov 20 '18 at 12:57













          Interesting! But what if you don't want to open all of the files using the set umask and use a specific file creation functionality that will be permissionless?

          – Drxxd
          Nov 20 '18 at 13:00





          Interesting! But what if you don't want to open all of the files using the set umask and use a specific file creation functionality that will be permissionless?

          – Drxxd
          Nov 20 '18 at 13:00













          You can set umask to 0, of course, either external to your code or, as @ShadowRanger has suggested, using os.umask.

          – larsks
          Nov 20 '18 at 13:06





          You can set umask to 0, of course, either external to your code or, as @ShadowRanger has suggested, using os.umask.

          – larsks
          Nov 20 '18 at 13:06













          @larsks: Agreed that usually the user should be in charge of their umask, not the program.

          – ShadowRanger
          Nov 20 '18 at 13:35





          @larsks: Agreed that usually the user should be in charge of their umask, not the program.

          – ShadowRanger
          Nov 20 '18 at 13:35




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390466%2fos-open-doesnt-create-files-with-the-given-permission-mode%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)