How to check whether a .txt file is empty
Clash 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?
files-and-directories
add a comment |Â
up vote
7
down vote
favorite
What is a quick (preferably OS independent) way to check whether a .txt file is empty using Mathematica?
files-and-directories
add a comment |Â
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?
files-and-directories
What is a quick (preferably OS independent) way to check whether a .txt file is empty using Mathematica?
files-and-directories
asked Aug 20 at 9:33
Kvothe
812215
812215
add a comment |Â
add a comment |Â
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"]
add a comment |Â
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!
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 usingFileNameJoin
, egFileNameJoin["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 usingImport
/Export
).
â Musang
Aug 20 at 15:46
add a comment |Â
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"]
add a comment |Â
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"]
add a comment |Â
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"]
FileByteCount
will give 0 for empty text files and is very fast (0.00003 seconds per file on my machine).
FileByteCount["~/test.txt"]
answered Aug 20 at 9:46
Carl Lange
828114
828114
add a comment |Â
add a comment |Â
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!
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 usingFileNameJoin
, egFileNameJoin["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 usingImport
/Export
).
â Musang
Aug 20 at 15:46
add a comment |Â
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!
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 usingFileNameJoin
, egFileNameJoin["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 usingImport
/Export
).
â Musang
Aug 20 at 15:46
add a comment |Â
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!
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!
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 usingFileNameJoin
, egFileNameJoin["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 usingImport
/Export
).
â Musang
Aug 20 at 15:46
add a comment |Â
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 usingFileNameJoin
, egFileNameJoin["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 usingImport
/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
add a comment |Â
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
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
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
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
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