Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you load an XML file into a string with python on GAE?

I'm using the latest GAE default python environment. Both of these give expected results:

isTrue = os.path.exists(path)
numberGreaterThanZero = os.path.getsize(path)

But this:

myStrLen = len(open(path))

Gives this error:

TypeError: object of type 'FakeFile' has no len()

There are no results for that error in Google. Not being able to open files is a real bummer. What am I doing wrong? Why does Python/GAE think my file is fake?

like image 300
Tom H Avatar asked May 23 '26 18:05

Tom H


1 Answers

The open function returns an open file, not a string. Open files have no len.

You need to actually read the string from the file, for example with the read method.

contents = open(path).read()
myStrLen = len(contents)

If you don't need the contents, you can also get the file size with os.stat.

myStrLen = os.stat('/tmp/x.py').st_size

FakeFile is just GAE's sandboxed implementation of file.

like image 103
Petr Viktorin Avatar answered May 25 '26 07:05

Petr Viktorin