How to check if a file is empty in Bash?
Misspellings are irritating, aren't they? Check your spelling of empty
, but then also try this:
#!/bin/bash -e
if [ -s diff.txt ]; then
# The file is not-empty.
rm -f empty.txt
touch full.txt
else
# The file is empty.
rm -f full.txt
touch empty.txt
fi
I like shell scripting a lot, but one disadvantage of it is that the shell cannot help you when you misspell, whereas a compiler like your C++ compiler can help you.
Notice incidentally that I have swapped the roles of empty.txt
and full.txt
, as @Matthias suggests.
How to check whether a file is empty or not
>>> import os
>>> os.stat("file").st_size == 0
True
How can I check if a file is empty or not? and empty it before writing if not?
If you want clear out any existing data in the file first and write your output whether the file was empty or not, that's easy: just use
open('out.txt', 'w')
'w'
means "write", 'a'
means "append".
If you want to check whether the file has data in it and not write anything at all if there is data, then Hypnos' answer would be appropriate. Check the file size and skip the file-writing code if the file is not empty.
Most efficient way to check if a file is empty in Java on Windows
Check if the first line of file is empty:
BufferedReader br = new BufferedReader(new FileReader("path_to_some_file"));
if (br.readLine() == null) {
System.out.println("No errors, and file empty");
}
check if file is empty or not in c
it does'nt work because you have to fopen it and fseek to the end first:
FILE *fptr;
if ( !( fptr = fopen( "saving.txt", "r" ))){
printf( "File could not be opened to retrieve your data from it.\n" );
}
else {
fseek(fptr, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(fptr);
if (len > 0) { //check if the file is empty or not.
rewind(fptr);
while ( !feof( fptr ) ){
fscanf( fptr, "%f\n", &p.burst_time );
AddProcess(&l,p.burst_time);
}
}
fclose( fptr );
}
How can I check if a file is empty?
You can use file.size
:
empty = filenames[file.size(filenames) == 0L]
file.size
is a shortcut for file.info
:
info = file.info(filenames)
empty = rownames(info[info$size == 0L, ])
Incidentally, there’s a better way of listing text files than using grep
: specify the pattern
argument to list.files
:
list.files(pattern = '\\.txt$')
Note that the pattern needs to be a regular expression, not a glob — and the same is true for grep
!
Check file empty or not
You may use this awk
for this:
awk 'NF {exit 1}' file && echo "empty" || echo "not empty"
Condition NF
will be true only if there is non-whitespace character in the file.
Related Topics
Scraping: Ssl: Certificate_Verify_Failed Error for Http://En.Wikipedia.Org
Case Insensitive Regular Expression Without Re.Compile
Django Set Default Form Values
How to Activate an Anaconda Environment
Pandas Timeseries Plot Setting X-Axis Major and Minor Ticks and Labels
Plotting a 3D Cube, a Sphere and a Vector in Matplotlib
Matplotlib: Format Axis Offset-Values to Whole Numbers or Specific Number
Python Glob Multiple Filetypes
Regular Expression Matching a Multiline Block of Text
Python's Equivalent of && (Logical-And) in an If-Statement
Matplotlib Make Tick Labels Font Size Smaller
Can Pandas Automatically Read Dates from a CSV File
How to Remove Non-Ascii Characters But Leave Periods and Spaces
Which Python Packages Offer a Stand-Alone Event System
Can "List_Display" in a Django Modeladmin Display Attributes of Foreignkey Fields