How to check whether a .txt file is empty

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












What is a quick (preferably OS independent) way to check whether a .txt file is empty using Mathematica?







share|improve this question
























    up vote
    7
    down vote

    favorite












    What is a quick (preferably OS independent) way to check whether a .txt file is empty using Mathematica?







    share|improve this question






















      up vote
      7
      down vote

      favorite









      up vote
      7
      down vote

      favorite











      What is a quick (preferably OS independent) way to check whether a .txt file is empty using Mathematica?







      share|improve this question












      What is a quick (preferably OS independent) way to check whether a .txt file is empty using Mathematica?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 20 at 9:33









      Kvothe

      812215




      812215




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          14
          down vote













          FileByteCount will give 0 for empty text files and is very fast (0.00003 seconds per file on my machine).



          FileByteCount["~/test.txt"]





          share|improve this answer



























            up vote
            5
            down vote













            FileByteCount will probably be the fastest way of checking strict emptiness. But what if you need to actually read the file (e.g. check integrity or catch some corner case)? Here's an alternative where the file content is checked, using Read. In general, streams are the fastest way to read and write data in external files.



            Generate your file list



            filelist=FileNames["path/to/directory/*.txt"]


            Read the first word (Word)/number (Number) of the file. If the file is empty, it should return the symbol EndOfFile.



            Map[Read[#, Word] &][filelist]


            This gives you the option to run some test on the contents of the first lines of the file. Don't forget to close the opened streams so that you don't read subsequent lines if you repeat the command.



            Map[Close][filelist]


            The only thing that is not cross-platform is the way the path to the working directory is written!






            share|improve this answer






















            • Regarding the directory path specification: As long as you're using forward slashes and relative paths/one of the predefined base directories (such as $InstallationDirectory), I don't think there is a platform dependency. Also, "pathtodirectory*.txt" should be "path\to\directory\*.txt", or better yet "path/to/directory/*.txt"
              – Lukas Lang
              Aug 20 at 12:03











            • @Lukas Lang Correct, I mixed my Windows and Unix paths again...
              – Musang
              Aug 20 at 12:07






            • 1




              You can also bypass the file path situation completely by using FileNameJoin, eg FileNameJoin["path", "to", "dir", "*.txt"], which will handle any OS-specific details for you.
              – Carl Lange
              Aug 20 at 13:09











            • Really bad idea to do a read; it'll kill the performance. The size of the file is likely recorded as part of the file metadata in the file system; meaning that no further disk access is required to establish if empty or not. Reading however will result in the hard disk access of the given sector for the file data, and establishing if there's no data there - if there is, you've done a completely pointless hard disk access.
              – UKMonkey
              Aug 20 at 15:09











            • @UKMonkey I don't claim that this answer will perform better than checking the file size. Perhaps I will make this more explicit in the opening sentence. Additional considerations I had when writing this answer: 1) Perhaps there can be a case where files with contents can still be considered empty (e.g file contains whitespace but is still considered empty). 2) Someone doing these kinds of file operations might be interested in efficient read/write methods in which case it can be useful to point them toward the lesser-known stream operations (as opposed to using Import/Export).
              – Musang
              Aug 20 at 15:46










            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "387"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180293%2fhow-to-check-whether-a-txt-file-is-empty%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            14
            down vote













            FileByteCount will give 0 for empty text files and is very fast (0.00003 seconds per file on my machine).



            FileByteCount["~/test.txt"]





            share|improve this answer
























              up vote
              14
              down vote













              FileByteCount will give 0 for empty text files and is very fast (0.00003 seconds per file on my machine).



              FileByteCount["~/test.txt"]





              share|improve this answer






















                up vote
                14
                down vote










                up vote
                14
                down vote









                FileByteCount will give 0 for empty text files and is very fast (0.00003 seconds per file on my machine).



                FileByteCount["~/test.txt"]





                share|improve this answer












                FileByteCount will give 0 for empty text files and is very fast (0.00003 seconds per file on my machine).



                FileByteCount["~/test.txt"]






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 20 at 9:46









                Carl Lange

                828114




                828114




















                    up vote
                    5
                    down vote













                    FileByteCount will probably be the fastest way of checking strict emptiness. But what if you need to actually read the file (e.g. check integrity or catch some corner case)? Here's an alternative where the file content is checked, using Read. In general, streams are the fastest way to read and write data in external files.



                    Generate your file list



                    filelist=FileNames["path/to/directory/*.txt"]


                    Read the first word (Word)/number (Number) of the file. If the file is empty, it should return the symbol EndOfFile.



                    Map[Read[#, Word] &][filelist]


                    This gives you the option to run some test on the contents of the first lines of the file. Don't forget to close the opened streams so that you don't read subsequent lines if you repeat the command.



                    Map[Close][filelist]


                    The only thing that is not cross-platform is the way the path to the working directory is written!






                    share|improve this answer






















                    • Regarding the directory path specification: As long as you're using forward slashes and relative paths/one of the predefined base directories (such as $InstallationDirectory), I don't think there is a platform dependency. Also, "pathtodirectory*.txt" should be "path\to\directory\*.txt", or better yet "path/to/directory/*.txt"
                      – Lukas Lang
                      Aug 20 at 12:03











                    • @Lukas Lang Correct, I mixed my Windows and Unix paths again...
                      – Musang
                      Aug 20 at 12:07






                    • 1




                      You can also bypass the file path situation completely by using FileNameJoin, eg FileNameJoin["path", "to", "dir", "*.txt"], which will handle any OS-specific details for you.
                      – Carl Lange
                      Aug 20 at 13:09











                    • Really bad idea to do a read; it'll kill the performance. The size of the file is likely recorded as part of the file metadata in the file system; meaning that no further disk access is required to establish if empty or not. Reading however will result in the hard disk access of the given sector for the file data, and establishing if there's no data there - if there is, you've done a completely pointless hard disk access.
                      – UKMonkey
                      Aug 20 at 15:09











                    • @UKMonkey I don't claim that this answer will perform better than checking the file size. Perhaps I will make this more explicit in the opening sentence. Additional considerations I had when writing this answer: 1) Perhaps there can be a case where files with contents can still be considered empty (e.g file contains whitespace but is still considered empty). 2) Someone doing these kinds of file operations might be interested in efficient read/write methods in which case it can be useful to point them toward the lesser-known stream operations (as opposed to using Import/Export).
                      – Musang
                      Aug 20 at 15:46














                    up vote
                    5
                    down vote













                    FileByteCount will probably be the fastest way of checking strict emptiness. But what if you need to actually read the file (e.g. check integrity or catch some corner case)? Here's an alternative where the file content is checked, using Read. In general, streams are the fastest way to read and write data in external files.



                    Generate your file list



                    filelist=FileNames["path/to/directory/*.txt"]


                    Read the first word (Word)/number (Number) of the file. If the file is empty, it should return the symbol EndOfFile.



                    Map[Read[#, Word] &][filelist]


                    This gives you the option to run some test on the contents of the first lines of the file. Don't forget to close the opened streams so that you don't read subsequent lines if you repeat the command.



                    Map[Close][filelist]


                    The only thing that is not cross-platform is the way the path to the working directory is written!






                    share|improve this answer






















                    • Regarding the directory path specification: As long as you're using forward slashes and relative paths/one of the predefined base directories (such as $InstallationDirectory), I don't think there is a platform dependency. Also, "pathtodirectory*.txt" should be "path\to\directory\*.txt", or better yet "path/to/directory/*.txt"
                      – Lukas Lang
                      Aug 20 at 12:03











                    • @Lukas Lang Correct, I mixed my Windows and Unix paths again...
                      – Musang
                      Aug 20 at 12:07






                    • 1




                      You can also bypass the file path situation completely by using FileNameJoin, eg FileNameJoin["path", "to", "dir", "*.txt"], which will handle any OS-specific details for you.
                      – Carl Lange
                      Aug 20 at 13:09











                    • Really bad idea to do a read; it'll kill the performance. The size of the file is likely recorded as part of the file metadata in the file system; meaning that no further disk access is required to establish if empty or not. Reading however will result in the hard disk access of the given sector for the file data, and establishing if there's no data there - if there is, you've done a completely pointless hard disk access.
                      – UKMonkey
                      Aug 20 at 15:09











                    • @UKMonkey I don't claim that this answer will perform better than checking the file size. Perhaps I will make this more explicit in the opening sentence. Additional considerations I had when writing this answer: 1) Perhaps there can be a case where files with contents can still be considered empty (e.g file contains whitespace but is still considered empty). 2) Someone doing these kinds of file operations might be interested in efficient read/write methods in which case it can be useful to point them toward the lesser-known stream operations (as opposed to using Import/Export).
                      – Musang
                      Aug 20 at 15:46












                    up vote
                    5
                    down vote










                    up vote
                    5
                    down vote









                    FileByteCount will probably be the fastest way of checking strict emptiness. But what if you need to actually read the file (e.g. check integrity or catch some corner case)? Here's an alternative where the file content is checked, using Read. In general, streams are the fastest way to read and write data in external files.



                    Generate your file list



                    filelist=FileNames["path/to/directory/*.txt"]


                    Read the first word (Word)/number (Number) of the file. If the file is empty, it should return the symbol EndOfFile.



                    Map[Read[#, Word] &][filelist]


                    This gives you the option to run some test on the contents of the first lines of the file. Don't forget to close the opened streams so that you don't read subsequent lines if you repeat the command.



                    Map[Close][filelist]


                    The only thing that is not cross-platform is the way the path to the working directory is written!






                    share|improve this answer














                    FileByteCount will probably be the fastest way of checking strict emptiness. But what if you need to actually read the file (e.g. check integrity or catch some corner case)? Here's an alternative where the file content is checked, using Read. In general, streams are the fastest way to read and write data in external files.



                    Generate your file list



                    filelist=FileNames["path/to/directory/*.txt"]


                    Read the first word (Word)/number (Number) of the file. If the file is empty, it should return the symbol EndOfFile.



                    Map[Read[#, Word] &][filelist]


                    This gives you the option to run some test on the contents of the first lines of the file. Don't forget to close the opened streams so that you don't read subsequent lines if you repeat the command.



                    Map[Close][filelist]


                    The only thing that is not cross-platform is the way the path to the working directory is written!







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 20 at 15:55

























                    answered Aug 20 at 10:36









                    Musang

                    6571517




                    6571517











                    • Regarding the directory path specification: As long as you're using forward slashes and relative paths/one of the predefined base directories (such as $InstallationDirectory), I don't think there is a platform dependency. Also, "pathtodirectory*.txt" should be "path\to\directory\*.txt", or better yet "path/to/directory/*.txt"
                      – Lukas Lang
                      Aug 20 at 12:03











                    • @Lukas Lang Correct, I mixed my Windows and Unix paths again...
                      – Musang
                      Aug 20 at 12:07






                    • 1




                      You can also bypass the file path situation completely by using FileNameJoin, eg FileNameJoin["path", "to", "dir", "*.txt"], which will handle any OS-specific details for you.
                      – Carl Lange
                      Aug 20 at 13:09











                    • Really bad idea to do a read; it'll kill the performance. The size of the file is likely recorded as part of the file metadata in the file system; meaning that no further disk access is required to establish if empty or not. Reading however will result in the hard disk access of the given sector for the file data, and establishing if there's no data there - if there is, you've done a completely pointless hard disk access.
                      – UKMonkey
                      Aug 20 at 15:09











                    • @UKMonkey I don't claim that this answer will perform better than checking the file size. Perhaps I will make this more explicit in the opening sentence. Additional considerations I had when writing this answer: 1) Perhaps there can be a case where files with contents can still be considered empty (e.g file contains whitespace but is still considered empty). 2) Someone doing these kinds of file operations might be interested in efficient read/write methods in which case it can be useful to point them toward the lesser-known stream operations (as opposed to using Import/Export).
                      – Musang
                      Aug 20 at 15:46
















                    • Regarding the directory path specification: As long as you're using forward slashes and relative paths/one of the predefined base directories (such as $InstallationDirectory), I don't think there is a platform dependency. Also, "pathtodirectory*.txt" should be "path\to\directory\*.txt", or better yet "path/to/directory/*.txt"
                      – Lukas Lang
                      Aug 20 at 12:03











                    • @Lukas Lang Correct, I mixed my Windows and Unix paths again...
                      – Musang
                      Aug 20 at 12:07






                    • 1




                      You can also bypass the file path situation completely by using FileNameJoin, eg FileNameJoin["path", "to", "dir", "*.txt"], which will handle any OS-specific details for you.
                      – Carl Lange
                      Aug 20 at 13:09











                    • Really bad idea to do a read; it'll kill the performance. The size of the file is likely recorded as part of the file metadata in the file system; meaning that no further disk access is required to establish if empty or not. Reading however will result in the hard disk access of the given sector for the file data, and establishing if there's no data there - if there is, you've done a completely pointless hard disk access.
                      – UKMonkey
                      Aug 20 at 15:09











                    • @UKMonkey I don't claim that this answer will perform better than checking the file size. Perhaps I will make this more explicit in the opening sentence. Additional considerations I had when writing this answer: 1) Perhaps there can be a case where files with contents can still be considered empty (e.g file contains whitespace but is still considered empty). 2) Someone doing these kinds of file operations might be interested in efficient read/write methods in which case it can be useful to point them toward the lesser-known stream operations (as opposed to using Import/Export).
                      – Musang
                      Aug 20 at 15:46















                    Regarding the directory path specification: As long as you're using forward slashes and relative paths/one of the predefined base directories (such as $InstallationDirectory), I don't think there is a platform dependency. Also, "pathtodirectory*.txt" should be "path\to\directory\*.txt", or better yet "path/to/directory/*.txt"
                    – Lukas Lang
                    Aug 20 at 12:03





                    Regarding the directory path specification: As long as you're using forward slashes and relative paths/one of the predefined base directories (such as $InstallationDirectory), I don't think there is a platform dependency. Also, "pathtodirectory*.txt" should be "path\to\directory\*.txt", or better yet "path/to/directory/*.txt"
                    – Lukas Lang
                    Aug 20 at 12:03













                    @Lukas Lang Correct, I mixed my Windows and Unix paths again...
                    – Musang
                    Aug 20 at 12:07




                    @Lukas Lang Correct, I mixed my Windows and Unix paths again...
                    – Musang
                    Aug 20 at 12:07




                    1




                    1




                    You can also bypass the file path situation completely by using FileNameJoin, eg FileNameJoin["path", "to", "dir", "*.txt"], which will handle any OS-specific details for you.
                    – Carl Lange
                    Aug 20 at 13:09





                    You can also bypass the file path situation completely by using FileNameJoin, eg FileNameJoin["path", "to", "dir", "*.txt"], which will handle any OS-specific details for you.
                    – Carl Lange
                    Aug 20 at 13:09













                    Really bad idea to do a read; it'll kill the performance. The size of the file is likely recorded as part of the file metadata in the file system; meaning that no further disk access is required to establish if empty or not. Reading however will result in the hard disk access of the given sector for the file data, and establishing if there's no data there - if there is, you've done a completely pointless hard disk access.
                    – UKMonkey
                    Aug 20 at 15:09





                    Really bad idea to do a read; it'll kill the performance. The size of the file is likely recorded as part of the file metadata in the file system; meaning that no further disk access is required to establish if empty or not. Reading however will result in the hard disk access of the given sector for the file data, and establishing if there's no data there - if there is, you've done a completely pointless hard disk access.
                    – UKMonkey
                    Aug 20 at 15:09













                    @UKMonkey I don't claim that this answer will perform better than checking the file size. Perhaps I will make this more explicit in the opening sentence. Additional considerations I had when writing this answer: 1) Perhaps there can be a case where files with contents can still be considered empty (e.g file contains whitespace but is still considered empty). 2) Someone doing these kinds of file operations might be interested in efficient read/write methods in which case it can be useful to point them toward the lesser-known stream operations (as opposed to using Import/Export).
                    – Musang
                    Aug 20 at 15:46




                    @UKMonkey I don't claim that this answer will perform better than checking the file size. Perhaps I will make this more explicit in the opening sentence. Additional considerations I had when writing this answer: 1) Perhaps there can be a case where files with contents can still be considered empty (e.g file contains whitespace but is still considered empty). 2) Someone doing these kinds of file operations might be interested in efficient read/write methods in which case it can be useful to point them toward the lesser-known stream operations (as opposed to using Import/Export).
                    – Musang
                    Aug 20 at 15:46












                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180293%2fhow-to-check-whether-a-txt-file-is-empty%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    這個網誌中的熱門文章

                    How to combine Bézier curves to a surface?

                    Why am i infinitely getting the same tweet with the Twitter Search API?

                    Is there any way to eliminate the singular point to solve this integral by hand or by approximations?