如何检查文件是否为空?

时间:2022-11-03 07:17:47

I have thousands of text files and would like to know how to check if a particular file is empty. I am reading all the files using this line of code

我有数千个文本文件,我想知道如何检查某个文件是否为空。我正在用这一行代码读取所有的文件

Y<-grep("*.txt", list.files(), value = TRUE)

I would like a list of names of all the blank files. Have to do it in R.

我想要一张所有空白文件的名单。必须用R表示。

Thanks.

谢谢。

2 个解决方案

#1


19  

You can use file.info for that:

你可以使用文件。info:

info = file.info(filenames)
empty = rownames(info[info$size == 0, ])

Incidentally, there’s a better way of listing text files than using grep: specify the pattern argument to list.files:

顺便说一句,列出文本文件的方法比使用grep:为list.files指定模式参数更好:

list.files(pattern = '\\.txt$')

Notice that the pattern needs to be a regular expression, not a glob – the same is true for grep!

请注意,模式需要是正则表达式,而不是glob——对于grep也是如此!

#2


0  

find . -empty 

or

find . -empty |awk -F\/ '{print $FN}'

If you want to limit just txt files:

如果你只想限制txt文件:

find . -empty -name "*.txt"

If you want only asci files (not just .txt)

如果您只想要asci文件(不只是.txt)

find . -empty -type f

put it all together:

把这一切放在一起:

find . -empty -type f -name "*.txt" |awk -F\/ '{print $NF}'

#1


19  

You can use file.info for that:

你可以使用文件。info:

info = file.info(filenames)
empty = rownames(info[info$size == 0, ])

Incidentally, there’s a better way of listing text files than using grep: specify the pattern argument to list.files:

顺便说一句,列出文本文件的方法比使用grep:为list.files指定模式参数更好:

list.files(pattern = '\\.txt$')

Notice that the pattern needs to be a regular expression, not a glob – the same is true for grep!

请注意,模式需要是正则表达式,而不是glob——对于grep也是如此!

#2


0  

find . -empty 

or

find . -empty |awk -F\/ '{print $FN}'

If you want to limit just txt files:

如果你只想限制txt文件:

find . -empty -name "*.txt"

If you want only asci files (not just .txt)

如果您只想要asci文件(不只是.txt)

find . -empty -type f

put it all together:

把这一切放在一起:

find . -empty -type f -name "*.txt" |awk -F\/ '{print $NF}'