check existance of file without import os?

(Abracsis) #1

ok i also need to check the existance of a file, but i can’t expect everyone who wants to play the game to have python installed and all the path stuff set up, else no one will bother.

therefore two methods seem possible wiht pure blender…

  1. if open(file, “r”):

this will return true if it can read a file. This therefore checks the existance of a file…However, if the file doesn’t exist, then it returns an error…

is there anyway to say, If an error happens… return false???

blah if only os was a pure blender python module

  1. would there be a way to create or just append to it, and then check if the file has an attribute it wasn’t set on creation but after this check. Therefore it will have a property if it existed before being created/appended?

I am not good at python, only good enough to unerstand my problem.

i have to check if a file exists…


(eeshlo) #2

For your first problem, you could try this:

	# try to open the file
	# 'r' (readmode) is default so doesn't have to be specified.
	file = open(filename)
except IOError:
	# could not open file, so it isn't there
	file_exists = 0
	# it was opened, so it exists
	file_exists = 1
	# since the file was opened, close it again

if file_exists:
	print "The file exists"
	print "Nothing there..."

For your second, I’m not sure what you are trying to do, do you want to test if a file has changed?
Appending to a file can be done with:
open(filename, ‘a’)

(RipSting) #3

is there anyway to say, If an error happens… return false???

Yes, try this:

...file = open(file,'r')
...print "successfull!"
...print "could not open!"