开发者

how to write or create (when no exist) a file using python and Google AppEngine

开发者 https://www.devze.com 2022-12-28 02:51 出处:网络
this is my code: f = open(\'text/a.log\', \'wb\') f.write(\'hahaha\') f.close() and it is not create a new file when not exist

this is my code:

f = open('text/a.log', 'wb')
f.write('hahaha')
f.close()

and it is not create a new file when not exist

how to do this ,

thanks

updated

class MyThread(threading.Thread):
开发者_JAVA百科    def run(self):
        f = open('a.log', 'w')
        f.write('hahaha')
        f.close()

error is :

Traceback (most recent call last):
  File "D:\Python25\lib\threading.py", line 486, in __bootstrap_inner
    self.run()
  File "D:\zjm_code\helloworld\views.py", line 15, in run
    f = open('a.log', 'w')
  File "d:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1188, in __init__
    raise IOError('invalid mode: %s' % mode)
IOError: invalid mode: w


You are using the Google App Engine.

From the Google App Engine documentation:

The Sandbox

Applications run in a secure environment that provides limited access to the underlying operating system. These limitations allow App Engine to distribute web requests for the application across multiple servers, and start and stop servers to meet traffic demands. The sandbox isolates your application in its own secure, reliable environment that is independent of the hardware, operating system and physical location of the web server. Examples of the limitations of the secure sandbox environment include:

  • An application cannot write to the file system. An app can read files, but only files uploaded with the application code. The app must use the App Engine datastore, memcache or other services for all data that persists between requests.


Its because of google appengine not allowed you to write files

its define like this

ALLOWED_MODES = frozenset(['r', 'rb', 'U', 'rU'])

and

if mode not in FakeFile.ALLOWED_MODES:
  raise IOError('invalid mode: %s' % mode)

Note: 'U' is universal newline mode, http://docs.python.org/library/io.html#io.open

Edit: You might interest Google AppEngine Logging session in their documents

Example

import logging
....
logging.error('There was an error retrieving ...')
logging.debug('Finish something')


You don't show the error you get; despite the community pleading you to do so here and in previous questions.

I expect you get an IOError because the text directory is not created yet.

Use something like this instead:

from __future__ import with_statement
import os

dir = 'text'
filename = 'a.log'
log_path = os.path.join(dir, filename)

if not os.path.exists(dir):
    os.makedirs(dir)

with open(log_path, 'w') as f:
    f.write("Nobody expects the Spanish inquisition!")

Notes:
Joining paths with slashes willy-nilly is a good way to write code that doesn't work cross-platform.

Open files using the with statement. The file is closed at the end of the with block. Use from __future__ import with_statement in versions <= 2.5


It seems most probably the relative directory path to 'text' not existing. Do check DIR path and then open file in write mode.


agreed with @bp and @S.Mark: App Engine does not allow you to create files... period. in addition to the page that @bp pointed out, it's documented in a few more places:

http://code.google.com/appengine/docs/java/overview.html http://code.google.com/appengine/docs/python/overview.html

specifically, those pages state, "...an app cannot spawn threads, write data to the local file system or make arbitrary network connections."

it's interesting that the OP tried 2 of the 3. :-)

however, if you want certain files, you need to upload them with your app, either individual files or a bunch thrown into a ZIP archive. finally, if you have really large files, you may wish to consider using the Blobstore (up to 50MB each):

http://code.google.com/appengine/docs/python/blobstore/ http://code.google.com/appengine/docs/java/blobstore/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号