os.open doesn't create files with the given permission mode
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
add a comment |
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
add a comment |
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
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
python linux chmod pathlib
edited Nov 20 '18 at 10:05
martineau
68k1090183
68k1090183
asked Nov 20 '18 at 10:00
DrxxdDrxxd
665415
665415
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
Note that Python includesos.umask
for manipulating theumask
.
– 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 setumask
to 0, of course, either external to your code or, as @ShadowRanger has suggested, usingos.umask
.
– larsks
Nov 20 '18 at 13:06
@larsks: Agreed that usually the user should be in charge of theirumask
, not the program.
– ShadowRanger
Nov 20 '18 at 13:35
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Note that Python includesos.umask
for manipulating theumask
.
– 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 setumask
to 0, of course, either external to your code or, as @ShadowRanger has suggested, usingos.umask
.
– larsks
Nov 20 '18 at 13:06
@larsks: Agreed that usually the user should be in charge of theirumask
, not the program.
– ShadowRanger
Nov 20 '18 at 13:35
add a comment |
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.
Note that Python includesos.umask
for manipulating theumask
.
– 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 setumask
to 0, of course, either external to your code or, as @ShadowRanger has suggested, usingos.umask
.
– larsks
Nov 20 '18 at 13:06
@larsks: Agreed that usually the user should be in charge of theirumask
, not the program.
– ShadowRanger
Nov 20 '18 at 13:35
add a comment |
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.
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.
answered Nov 20 '18 at 12:50
larskslarsks
118k19197205
118k19197205
Note that Python includesos.umask
for manipulating theumask
.
– 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 setumask
to 0, of course, either external to your code or, as @ShadowRanger has suggested, usingos.umask
.
– larsks
Nov 20 '18 at 13:06
@larsks: Agreed that usually the user should be in charge of theirumask
, not the program.
– ShadowRanger
Nov 20 '18 at 13:35
add a comment |
Note that Python includesos.umask
for manipulating theumask
.
– 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 setumask
to 0, of course, either external to your code or, as @ShadowRanger has suggested, usingos.umask
.
– larsks
Nov 20 '18 at 13:06
@larsks: Agreed that usually the user should be in charge of theirumask
, 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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