user2399453
user2399453

Reputation: 3081

Google app engine os.path functions not working

I am seeing a strange and frustrating behavior in Google App Engine. I am launching google app engine from a particular folder like this: (dev_appserver .) In this folder I have a folder called data with a few subfolders under it (one of them is json_folder which contains a file temp.json). From inside my main python src that contains the MainHandler class I do this:

 print os.getcwd(), os.path.isfile("data/json_folder/temp.json")

It prints the expected cwd and False even though the json_folder/temp.json exists and if I launch a regular python shell from the same directory it correctly prints True. Why does it work in regular Python but not in GAE python?

I also tried the following. I walk through my current dir and list the subfolders under data but the isdir() returns false even for directories! Why is python thinking they are not directories? ls -al shows them to be directories it makes no sense: (this prints json_folder, False as one of the outputs, all subfolders are returning False):

for root, dirs, files in os.walk("data"): 
   for file in files: 
      print file, os.path.isdir(file)

Upvotes: 1

Views: 373

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180481

You need to os.path.join the path, it works if you only use files from the cwd as you are in the same directory as the file, once you hit a file folder outside the cwd you are checking if the file from that outside directory is a file in your cwd, you also want isfile to check for files:

 print file, os.path.isfile(os.path.join(root, file))

If you want to check for directories you need to iterate over dirs not files:

for root, dirs, files in os.walk("data"): 
   for _dir in dirs: 
          print _dir, os.path.isdir(os.path.join(root, _dir)

Upvotes: 1

Related Questions