Is os.path.isfile always the opposite of os.path.isdir for an existing file system object?
I am to list all the files and directories in a directory with os.listdir
and tell them apart reliably. Is it ok to use just os.path.isdir
and consider it's a file if it returns false or should I check os.path.isfile
anyway? Are there cases when os.path.exists(path) and os.path.isdir(path) == os.path.isfile(path)
happens to be true?
python os.path
add a comment |
I am to list all the files and directories in a directory with os.listdir
and tell them apart reliably. Is it ok to use just os.path.isdir
and consider it's a file if it returns false or should I check os.path.isfile
anyway? Are there cases when os.path.exists(path) and os.path.isdir(path) == os.path.isfile(path)
happens to be true?
python os.path
1
Depending on your OS and file system there can be more than just files and directories.
– Klaus D.
Nov 26 '18 at 4:03
@KlausD. That's why I'm asking. Any examples?
– Ivan
Nov 26 '18 at 4:14
For example on Linux you have devices, socket, named pipes...
– Klaus D.
Nov 26 '18 at 4:37
add a comment |
I am to list all the files and directories in a directory with os.listdir
and tell them apart reliably. Is it ok to use just os.path.isdir
and consider it's a file if it returns false or should I check os.path.isfile
anyway? Are there cases when os.path.exists(path) and os.path.isdir(path) == os.path.isfile(path)
happens to be true?
python os.path
I am to list all the files and directories in a directory with os.listdir
and tell them apart reliably. Is it ok to use just os.path.isdir
and consider it's a file if it returns false or should I check os.path.isfile
anyway? Are there cases when os.path.exists(path) and os.path.isdir(path) == os.path.isfile(path)
happens to be true?
python os.path
python os.path
asked Nov 26 '18 at 3:36
IvanIvan
27.1k74205336
27.1k74205336
1
Depending on your OS and file system there can be more than just files and directories.
– Klaus D.
Nov 26 '18 at 4:03
@KlausD. That's why I'm asking. Any examples?
– Ivan
Nov 26 '18 at 4:14
For example on Linux you have devices, socket, named pipes...
– Klaus D.
Nov 26 '18 at 4:37
add a comment |
1
Depending on your OS and file system there can be more than just files and directories.
– Klaus D.
Nov 26 '18 at 4:03
@KlausD. That's why I'm asking. Any examples?
– Ivan
Nov 26 '18 at 4:14
For example on Linux you have devices, socket, named pipes...
– Klaus D.
Nov 26 '18 at 4:37
1
1
Depending on your OS and file system there can be more than just files and directories.
– Klaus D.
Nov 26 '18 at 4:03
Depending on your OS and file system there can be more than just files and directories.
– Klaus D.
Nov 26 '18 at 4:03
@KlausD. That's why I'm asking. Any examples?
– Ivan
Nov 26 '18 at 4:14
@KlausD. That's why I'm asking. Any examples?
– Ivan
Nov 26 '18 at 4:14
For example on Linux you have devices, socket, named pipes...
– Klaus D.
Nov 26 '18 at 4:37
For example on Linux you have devices, socket, named pipes...
– Klaus D.
Nov 26 '18 at 4:37
add a comment |
3 Answers
3
active
oldest
votes
os.path.isdir and os.path.isfile both are ok! os.path.exists(path) and os.path.isdir(path) == os.path.isfile(path) is always False
add a comment |
You should be all good to just use os.path.isdir
. This only looks for if the path that is inputted is a directory. Otherwise, it is okay to assume it is a file. I have tested to see if any cases of whenos.path.exists(path) and os.path.isdir(path) == os.path.isfile(path)
Here are the results.
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
True, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, False, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\test"))
False, False, False,
As you can see, there are some cases that can relate both of os.path.isdir and os.path.exists equal to os.path.isfile
add a comment |
os.path.isdir(path) == os.path.isfile(path)
should never hold in all disk file systems I am aware of, as this should mean that the same object is both a dir and a file. For EXT4 specifically, it is my understanding that an inode can either be a directory or a file.
However, the two functions are not defined as mutually exclusive as that would require an assumption about this being true in all possible filesystems, including future ones, and predictions about that are hard.
What are the unusual cases when it may?
– Ivan
Nov 26 '18 at 4:12
Iff you are applyingfilename.lower()
your filenames inos.listdir('directory')
's output, it can cause some hiccups. Since, in Ubuntu (16.04.4) you can do stuff likemkdir Wtf; touch wtf;
in the same directory...
– khan
Nov 26 '18 at 4:20
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%2f53474489%2fis-os-path-isfile-always-the-opposite-of-os-path-isdir-for-an-existing-file-syst%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
os.path.isdir and os.path.isfile both are ok! os.path.exists(path) and os.path.isdir(path) == os.path.isfile(path) is always False
add a comment |
os.path.isdir and os.path.isfile both are ok! os.path.exists(path) and os.path.isdir(path) == os.path.isfile(path) is always False
add a comment |
os.path.isdir and os.path.isfile both are ok! os.path.exists(path) and os.path.isdir(path) == os.path.isfile(path) is always False
os.path.isdir and os.path.isfile both are ok! os.path.exists(path) and os.path.isdir(path) == os.path.isfile(path) is always False
answered Nov 26 '18 at 3:59
这个冬天有点冷灬这个冬天有点冷灬
262
262
add a comment |
add a comment |
You should be all good to just use os.path.isdir
. This only looks for if the path that is inputted is a directory. Otherwise, it is okay to assume it is a file. I have tested to see if any cases of whenos.path.exists(path) and os.path.isdir(path) == os.path.isfile(path)
Here are the results.
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
True, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, False, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\test"))
False, False, False,
As you can see, there are some cases that can relate both of os.path.isdir and os.path.exists equal to os.path.isfile
add a comment |
You should be all good to just use os.path.isdir
. This only looks for if the path that is inputted is a directory. Otherwise, it is okay to assume it is a file. I have tested to see if any cases of whenos.path.exists(path) and os.path.isdir(path) == os.path.isfile(path)
Here are the results.
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
True, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, False, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\test"))
False, False, False,
As you can see, there are some cases that can relate both of os.path.isdir and os.path.exists equal to os.path.isfile
add a comment |
You should be all good to just use os.path.isdir
. This only looks for if the path that is inputted is a directory. Otherwise, it is okay to assume it is a file. I have tested to see if any cases of whenos.path.exists(path) and os.path.isdir(path) == os.path.isfile(path)
Here are the results.
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
True, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, False, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\test"))
False, False, False,
As you can see, there are some cases that can relate both of os.path.isdir and os.path.exists equal to os.path.isfile
You should be all good to just use os.path.isdir
. This only looks for if the path that is inputted is a directory. Otherwise, it is okay to assume it is a file. I have tested to see if any cases of whenos.path.exists(path) and os.path.isdir(path) == os.path.isfile(path)
Here are the results.
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
True, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, True, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\"))
False, False, False
print(os.path.isdir("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.exists("C:\Users\Kobe Thompson\Desktop\Test\test"))
print(os.path.isfile("C:\Users\Kobe Thompson\Desktop\Test\test"))
False, False, False,
As you can see, there are some cases that can relate both of os.path.isdir and os.path.exists equal to os.path.isfile
answered Nov 26 '18 at 4:02
exeexe
13514
13514
add a comment |
add a comment |
os.path.isdir(path) == os.path.isfile(path)
should never hold in all disk file systems I am aware of, as this should mean that the same object is both a dir and a file. For EXT4 specifically, it is my understanding that an inode can either be a directory or a file.
However, the two functions are not defined as mutually exclusive as that would require an assumption about this being true in all possible filesystems, including future ones, and predictions about that are hard.
What are the unusual cases when it may?
– Ivan
Nov 26 '18 at 4:12
Iff you are applyingfilename.lower()
your filenames inos.listdir('directory')
's output, it can cause some hiccups. Since, in Ubuntu (16.04.4) you can do stuff likemkdir Wtf; touch wtf;
in the same directory...
– khan
Nov 26 '18 at 4:20
add a comment |
os.path.isdir(path) == os.path.isfile(path)
should never hold in all disk file systems I am aware of, as this should mean that the same object is both a dir and a file. For EXT4 specifically, it is my understanding that an inode can either be a directory or a file.
However, the two functions are not defined as mutually exclusive as that would require an assumption about this being true in all possible filesystems, including future ones, and predictions about that are hard.
What are the unusual cases when it may?
– Ivan
Nov 26 '18 at 4:12
Iff you are applyingfilename.lower()
your filenames inos.listdir('directory')
's output, it can cause some hiccups. Since, in Ubuntu (16.04.4) you can do stuff likemkdir Wtf; touch wtf;
in the same directory...
– khan
Nov 26 '18 at 4:20
add a comment |
os.path.isdir(path) == os.path.isfile(path)
should never hold in all disk file systems I am aware of, as this should mean that the same object is both a dir and a file. For EXT4 specifically, it is my understanding that an inode can either be a directory or a file.
However, the two functions are not defined as mutually exclusive as that would require an assumption about this being true in all possible filesystems, including future ones, and predictions about that are hard.
os.path.isdir(path) == os.path.isfile(path)
should never hold in all disk file systems I am aware of, as this should mean that the same object is both a dir and a file. For EXT4 specifically, it is my understanding that an inode can either be a directory or a file.
However, the two functions are not defined as mutually exclusive as that would require an assumption about this being true in all possible filesystems, including future ones, and predictions about that are hard.
edited Nov 26 '18 at 8:58
ntg
4,33143351
4,33143351
answered Nov 26 '18 at 4:02
khankhan
1,96683052
1,96683052
What are the unusual cases when it may?
– Ivan
Nov 26 '18 at 4:12
Iff you are applyingfilename.lower()
your filenames inos.listdir('directory')
's output, it can cause some hiccups. Since, in Ubuntu (16.04.4) you can do stuff likemkdir Wtf; touch wtf;
in the same directory...
– khan
Nov 26 '18 at 4:20
add a comment |
What are the unusual cases when it may?
– Ivan
Nov 26 '18 at 4:12
Iff you are applyingfilename.lower()
your filenames inos.listdir('directory')
's output, it can cause some hiccups. Since, in Ubuntu (16.04.4) you can do stuff likemkdir Wtf; touch wtf;
in the same directory...
– khan
Nov 26 '18 at 4:20
What are the unusual cases when it may?
– Ivan
Nov 26 '18 at 4:12
What are the unusual cases when it may?
– Ivan
Nov 26 '18 at 4:12
Iff you are applying
filename.lower()
your filenames in os.listdir('directory')
's output, it can cause some hiccups. Since, in Ubuntu (16.04.4) you can do stuff like mkdir Wtf; touch wtf;
in the same directory...– khan
Nov 26 '18 at 4:20
Iff you are applying
filename.lower()
your filenames in os.listdir('directory')
's output, it can cause some hiccups. Since, in Ubuntu (16.04.4) you can do stuff like mkdir Wtf; touch wtf;
in the same directory...– khan
Nov 26 '18 at 4:20
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%2f53474489%2fis-os-path-isfile-always-the-opposite-of-os-path-isdir-for-an-existing-file-syst%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
1
Depending on your OS and file system there can be more than just files and directories.
– Klaus D.
Nov 26 '18 at 4:03
@KlausD. That's why I'm asking. Any examples?
– Ivan
Nov 26 '18 at 4:14
For example on Linux you have devices, socket, named pipes...
– Klaus D.
Nov 26 '18 at 4:37