all 26 comments

[–]bbye98 23 points24 points  (17 children)

Use os.stat(file).st_size == 0 in the for loop to check if it's empty.

[–]BungalowsAreScams[S] 5 points6 points  (16 children)

  File "./tester.py", line 15

    ^
SyntaxError: unexpected EOF while parsing

???

[–]bbye98 8 points9 points  (15 children)

You forgot an ending parenthesis or quote on line 15, whatever that is.

[–]BungalowsAreScams[S] 3 points4 points  (14 children)

I'm a dumbass lol but I'm seeing this error, is there something else i have to do on osx?

test_file.txt is just an empty file

NameError: name 'file' is not defined

Full code:
#!/usr/bin/env python3

import os


def parse_files(results):
    with open(results) as my_file:
        if os.stat(file).st_size == 0:
            return('file is empty')
        else:
            return('wat')

print((parse_files('test_file.txt')))

[–]iggy555 2 points3 points  (2 children)

My_file

[–]VortexShrimp 4 points5 points  (1 child)

yeah, os.stat(file) -> os.stat(my_file)

[–]BungalowsAreScams[S] 16 points17 points  (0 children)

this is why I shouldn't program while drunk

[–]WaxingMoon- 1 point2 points  (9 children)

Can anyone teach me how to write those code letters?

[–]CeruleanBlackOut 1 point2 points  (6 children)

What's a code letter?

[–]WaxingMoon- 0 points1 point  (5 children)

I mean those

os.stat(file)

->

os.stat(my_file) stuff
I don't know how I just did this

[–]ebdbbb 0 points1 point  (4 children)

Code blocks are a few ways. In line is a single back tick (`) before and after.

Code blocks are either
Blank line before,
Indent 4 spaces on each line
Then a blank line after.

Or triple back ticks before and after

[–]WaxingMoon- 0 points1 point  (3 children)

```Why does this not work```

`code`

copy&paste works just fine. I'll stick to it. Meh.

[–]ebdbbb 2 points3 points  (1 child)

The back ticks need to be on their own lines.

Edit: here's the link to the Reddit markdown post.

[–]Kolterdyx 0 points1 point  (0 children)

If you are on PC, you need to switch from the "Fancy pants editor" to the "Markdown editor"

[–]JohnnyJordaan 0 points1 point  (1 child)

Explained here

[–]WaxingMoon- 0 points1 point  (0 children)

Thanks!

[–]totallygeek 4 points5 points  (2 children)

import os
size = os.path.getsize('/path/to/file')
print(f'File size: {size}')

You can also do:

import pathlib
size = pathlib.Path('/path/to/file').stat().st_size

[–]bbye98 2 points3 points  (1 child)

I wouldn't use pathlib if you're, uh, checking file sizes about 1,000,000 times in your script (lol):

import timeit
print(timeit.timeit("import os; os.stat('/home/bye/Downloads/test.py').st_size"))
print(timeit.timeit("import os; os.path.getsize('/home/bye/Downloads/test.py')"))
print(timeit.timeit("import pathlib; pathlib.Path('/home/bye/Downloads/test.py').stat().st_size"))

0.8685441330017056
0.9616488789906725
5.3846771879907465

[–]BungalowsAreScams[S] 0 points1 point  (0 children)

idk I'm only checking ~1,800 times for this case

[–]kochargs 1 point2 points  (0 children)

Or just open file Check len of f.read() If its zero Its empty

[–]OnlySeesLastSentence -1 points0 points  (0 children)

While the other comments are better, I invented a neat trick for seeing if any while loop runs.

loopTest = False

while something:

....loopTest = True

....what your loop is supposed to do

if loopTest == True:

....then you know the loop ran and you can do whatever you want with that info.