How can I check if a file is empty?

后端 未结 4 2063
春和景丽
春和景丽 2021-02-18 18:51

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(\         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-18 19:46

    You can use file.info for that:

    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:

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

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

提交回复
热议问题