How to check text file exists and is not empty in python
Asked Answered
A

4

36

I wrote a script to read text file in python.

Here is the code.

parser = argparse.ArgumentParser(description='script')    
parser.add_argument('-in', required=True, help='input file',
type=argparse.FileType('r'))
parser.add_argument('-out', required=True, help='outputfile',
type=argparse.FileType('w'))     
args = parser.parse_args()    

try:
    reader = csv.reader(args.in)
    for row in reader:
        print "good"
except csv.Error as e:
    sys.exit('file %s, line %d: %s' % (args.in, reader.line_num, e))

for ln in args.in:
    a, b = ln.rstrip().split(':')

I would like to check if the file exists and is not empty file but this code gives me an error.

I would also like to check if program can write to output file.

Command:

python script.py -in file1.txt -out file2.txt 

ERROR:

good
Traceback (most recent call last):
  File "scritp.py", line 80, in <module>
    first_cluster = clusters[0]
IndexError: list index out of range
Alexandraalexandre answered 26/2, 2015 at 8:10 Comment(4)
Check this link: #2259882Antihelix
that code does not even parse, in is not a valid identifier (in args.in)Jocko
Where does first_cluster = clusters[0] appear in your code?Bowing
The script gives an error because the FOR loop gets failed. When it start reading file it failed to read file from args.in . how do i read file using argument parser?Alexandraalexandre
E
63

To check whether file is present and is not empty, you need to call the combination of os.path.exists and os.path.getsize with the "and" condition. For example:

import os
my_path = "/path/to/file"

if os.path.exists(my_path) and os.path.getsize(my_path) > 0:
    # Non empty file exists
    # ... your code ...
else:
    # ... your code for else case ...

As an alternative, you may also use try/except with the os.path.getsize (without using os.path.exists) because it raises OSError if the file does not exist or if you do not have the permission to access the file. For example:

try:
    if os.path.getsize(my_path) > 0:
        # Non empty file exists
        # ... your code ...
    else:
        # Empty file exists
        # ... your code ...
except OSError as e:
    # File does not exists or is non accessible
    # ... your code ...

References from the Python 3 document

  • os.path.getsize() will:

    Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible.

    For empty file, it will return 0. For example:

    >>> import os
    >>> os.path.getsize('README.md')
    0
    
  • whereas os.path.exists(path) will:

    Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links.

    On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.

Epizoon answered 26/2, 2015 at 8:14 Comment(1)
I use this to check if I must download a file: must_be_downloaded = not os.path.isfile(file_path) or os.path.getsize(file_path) == 0 and it works w/o try/catch in python 3.6. Or use the opposite: exists_for_real = os.path.isfile(file_path) and os.path.getsize(file_path) > 0 you can make your own checked and add more conditions, but I just scripted the same and this simple check works well in my case.Zabaglione
O
6

On Python3 you should use pathlib.Path features for this purpose:

import pathlib as p
path = p.Path(f)
if path.exists() and path.stat().st_size > 0:
   raise RuntimeError("file exists and is not empty")

As you see the Path object contains all the functionality needed to perform the task.

Offhand answered 30/4, 2021 at 16:48 Comment(0)
U
2
def exist_and_not_empty(filepath):
    try:
        import pathlib as p
        path = p.Path(filepath)
        if '~' in filepath:
            path = path.expanduser()
        if not path.exists() and path.stat().st_size > 0:
            return False
        return True
    except FileNotFoundError:
        return False

This leverages all of the suggestions above and accounts for missing files and autoexpands tilde if detected so it just works.

Uncurl answered 29/5, 2021 at 14:50 Comment(0)
H
0

You may want to try this:

def existandnotempty(fp):
    if not os.path.isfile(fp):
        retun False
    k=0
    with open(fp,'r') as f:
        for l in f:
          k+=len(l)
          if k:
             return False
          k+=1
    return True 
Helli answered 31/7, 2020 at 13:59 Comment(1)
You can modify this as please. For exmple, you can use ' trim() ' if you dont want tabs, spaces, etc to be counted as "something". You can also remove the k+=1 if want to count as "empty" a file with only empty lines. In this form, any character ( even just a CR) would flag your file as non-empty.Helli

© 2022 - 2024 — McMap. All rights reserved.