tags:

views:

45

answers:

3

beginner question. here's pdb output

(Pdb) entry.image
<FieldFile: None>
(Pdb) entry.image is None
False

how do I check if the image exists or not?

Lets say I want to access entry.image.file but I dont know whether entry.image exists

SOLVED check my own answer

A: 

Here:

file_path = entry.image.path
open(file_path)

If the file exists, there should not be any error.

Lakshman Prasad
yes, but I asked how to check the existence of the file. It will give a ValueError ('has no file associated with it') if you try accessing the path or file attribute from entry.image
kusut
ksut, Well, you can always wrap the access statement in a try except.
Lakshman Prasad
yea true. I didnt think it that way back then
kusut
A: 

How about:

import os
os.path.exists(file_path) and os.path.isfile(file_path)
Manoj Govindan
check prev answer. accessing 'file' or 'path' attribute from the entry.image wont work
kusut
@Kusut: My mistake. Let me dig around a bit.
Manoj Govindan
A: 

Found out how to do it

bool(entry.image)
kusut
You should accept your own answer so that those who come after you will know what to do :)
Manoj Govindan
I did but she said "You can accept your own answer in 2 days" :). dont know why, I didnt have to wait to do that before
kusut