Is os.path.isfile always the opposite of os.path.isdir for an existing file system object?












0















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?










share|improve this question


















  • 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


















0















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?










share|improve this question


















  • 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
















0












0








0


0






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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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














3 Answers
3






active

oldest

votes


















1














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






share|improve this answer































    1














    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






    share|improve this answer































      0














      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.






      share|improve this answer


























      • 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











      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%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









      1














      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






      share|improve this answer




























        1














        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






        share|improve this answer


























          1












          1








          1







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 3:59









          这个冬天有点冷灬这个冬天有点冷灬

          262




          262

























              1














              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






              share|improve this answer




























                1














                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






                share|improve this answer


























                  1












                  1








                  1







                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 4:02









                  exeexe

                  13514




                  13514























                      0














                      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.






                      share|improve this answer


























                      • 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
















                      0














                      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.






                      share|improve this answer


























                      • 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














                      0












                      0








                      0







                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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 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



















                      • 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

















                      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


















                      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%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





















































                      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

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)