10
$\begingroup$

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

$\endgroup$
7
$\begingroup$

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!

$\endgroup$
  • $\begingroup$ 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, "path\to\directory\*.txt" should be "path\\to\\directory\\*.txt", or better yet "path/to/directory/*.txt" $\endgroup$ – Lukas Lang Aug 20 '18 at 12:03
  • $\begingroup$ @Lukas Lang Correct, I mixed my Windows and Unix paths again... $\endgroup$ – Musang Aug 20 '18 at 12:07
  • 2
    $\begingroup$ 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. $\endgroup$ – Carl Lange Aug 20 '18 at 13:09
  • $\begingroup$ 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. $\endgroup$ – UKMonkey Aug 20 '18 at 15:09
  • $\begingroup$ @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). $\endgroup$ – Musang Aug 20 '18 at 15:46
17
$\begingroup$

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

FileByteCount["~/test.txt"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.