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

          鏡平學校

          ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

          Why https connections are so slow when debugging (stepping over) in Java?