2

I am trying to read text files in a directory. However before doing any action I want to make sure it is not empty. If the text file is empty I need to create that empty file again (in the current directory that is), if not I need to perform some calculations with the each line of the text file and create corresponding file with same name. In short, I need to create as many files as the referred directory (path_in) has.

So to check if the text file is empty I thought I should open it first. I opened it with open and its handle is filename_handle. I checked if it is empty, but here it raises TypeError: argument should be string, bytes or integer, not _io.TextIOWrapper. I know why this error occured (because filename_handle is object), but I don't know the other way of how I can check if the file is empty.

Can someone help me how to fix this.

Here is my code

import numpy as np
import cv2, os
from glob import glob

path_in = 'C:\\Users\\user\\Desktop\\labels'

for filename in os.listdir(path_in):
    filename_edited = []
    with open(path_in + '\\' +filename) as filename_handle:

        if os.stat(filename_handle).st_size == 0:
            filename_edited.append(filename_handle)
        else:
            for line in filename_handle:
                numericdata = line.split(' ')
                numbers = []
                for i in numericdata:
                    numbers.append(int(i))
                c,x,y = numbers
                edited = [c, y, (19-x)]
                filename_edited.append(edited)
                filename_edited_array = np.array(filename_edited)

        with open(filename , 'wb') as f:
            np.savetxt(f, filename_edited_array,fmt= '%.1i', delimiter=' ', newline='\n', header='', footer='', comments='# ')

        continue
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
bit_scientist
  • 1,496
  • 2
  • 15
  • 34
  • @cᴏʟᴅsᴘᴇᴇᴅ it is not what I am asking, in that case it reads a file as string and outputs `FileNotFoundError` – bit_scientist Mar 30 '18 at 02:06

2 Answers2

1

You can use the os.path.getsize() method to get the size in bytes of the file you pass as argument for it. If the file is empty, it's size will be 0 bytes. So you can use the code below to check it for you:

import os
if os.path.getsize('path/to/file.txt') == 0: # If the file size is 0 bytes
    # Implementation
Ronald Pereira
  • 397
  • 1
  • 3
  • 12
1

os.stat takes the file name or the integer file descriptor. It doesn't accept arbitrary file objects.

So you need to either do:

os.stat(filename_handle.fileno()).st_size

which extracts the file descriptor from the open file, or:

os.stat(path_in + '\\' +filename).st_size

which stats the file by name, with needing an open file handle. More properly, you should really be using os.path.join to build paths, so os.stat(os.path.join(path_in, filename)).st_size would be the cleaner approach when doing this by name.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • I didn't try it @ShadowRanger, the answerby Ronald Pereira was enough to do the job. Thank you anyway! – bit_scientist Mar 30 '18 at 02:23
  • os.scandir() would be more consistent with his program. – Bit Chaser Mar 30 '18 at 02:23
  • @bitchaser: If you can use it, `os.scandir` is generally better for *every* program; it's faster and more convenient in general (if nothing else, it makes it trivial to get either the name or qualified path easily without string manipulation, and lets you process huge directories file by file without minutes long delays accumulating files). I'm leery of recommending stuff only available (built in) in 3.5+ quite yet though, especially when it's quite a tangent from the main question, because invariably the "3.x" ends up being some archaic version like 3.3 or 3.4 that lacks it. – ShadowRanger Mar 30 '18 at 02:25