check if a file is available

(Octopus4) #1

i want my game when at the begining , checks if a file is available then A happens and if the file isn’t avlb . then B happens .

and is there a way to compare , to see if the file size is 2 kilos then do A if not do B

how , please … i’m new to python in GE

0 Likes

(Nigholith) #2
import os
if os.stat("file.exe").st_size == 7401472L:
    print "File Size: 7,MB 401,KB 472,BY"
else: print "File wrong size"

This will use the working directory. Use “print os.getcwd()” to get the working directory.

As for checking if a file exists, I’m not sure.

0 Likes

(animatinator) #3

To check if it exists, try this:


try:
    file = open("<the file's location on your computer>", "r")

    # Do the stuff you want it to do with the file here

    file.close()

except:

    # Do this if the file's not there


It tries to open the file in read mode, and if it can’t do that, it goes on the the “except:” bit.

0 Likes

(Octopus4) #4

ani & nigo , thanks …

0 Likes