如何使用python和Google AppEngine编写或创建文件(如果不存在)
问题描述:
这是我的代码:
f = open('text/a.log', 'wb')
f.write('hahaha')
f.close()
它不存在时不会创建新文件
and it is not create a new file when not exist
如何做到这一点,
谢谢
已更新
class MyThread(threading.Thread):
def run(self):
f = open('a.log', 'w')
f.write('hahaha')
f.close()
错误是:
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
答
由于Google Appengine不允许您写入文件
Its because of google appengine not allowed you to write files
它的定义是这样的
ALLOWED_MODES = frozenset(['r', 'rb', 'U', 'rU'])
和
if mode not in FakeFile.ALLOWED_MODES:
raise IOError('invalid mode: %s' % mode)
注意:"U"是通用换行模式, http://docs.python.org/library/io.html#io.open
Note: 'U' is universal newline mode, http://docs.python.org/library/io.html#io.open
修改:您可能会对Google AppEngine 登录感兴趣>他们文档中的会话
Edit: You might interest Google AppEngine Logging session in their documents
示例
import logging
....
logging.error('There was an error retrieving ...')
logging.debug('Finish something')