How to check whether a .txt file is empty
How to check whether a .txt file is empty
What is a quick (preferably OS independent) way to check whether a .txt file is empty using Mathematica?
2 Answers
2
FileByteCount
will give 0 for empty text files and is very fast (0.00003 seconds per file on my machine).
FileByteCount
FileByteCount["~/test.txt"]
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.
FileByteCount
Read
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
.
Word
Number
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!
$InstallationDirectory
"pathtodirectory*.txt"
"path\to\directory\*.txt"
"path/to/directory/*.txt"
@Lukas Lang Correct, I mixed my Windows and Unix paths again...
– Musang
Aug 20 at 12:07
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
FileNameJoin
FileNameJoin["path", "to", "dir", "*.txt"]
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
Import
Export
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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