How to write or create (when not) a file using python and Google AppEngine

this is my code:

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

and it does not create a new file if it does not exist

how to do it,

thank

updated

class MyThread(threading.Thread):
    def run(self):
        f = open('a.log', 'w')
        f.write('hahaha')
        f.close()

error:

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
+3
source share
5 answers

Due to the fact that Google appengine did not allow you to write files

it is defined 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 the universal newline mode, http://docs.python.org/library/io.html#io.open

Change . You may be interested in Google AppEngine Logging in your docs.

Example

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

You are using the Google App Engine.

Google App Engine:

, . App Engine - . , , -. :

  • . , , . App Engine, memcache , .
+9

; , .

, IOError, .

- :

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!")

:
- - , .

with. with. from __future__ import with_statement <= 2.5

+3

, , "text" . DIR .

0

@bp @S.Mark: App Engine ... . , @bp, :

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

, : "... , ".

, 2 3.: -)

however, if you need certain files, you need to download them using your application, or individual files, or a put that are abandoned in a ZIP archive. finally, if you have really large files, you might want to use Blobstore (up to 50 MB each):

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

0
source

Source: https://habr.com/ru/post/1742056/


All Articles