views:

403

answers:

5

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):
    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
+3  A: 

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

Adam Bernier
sorry, i was sleeping just now ,see the updated
zjm1126
A: 

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

aberry
+4  A: 

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')
S.Mark
You sir are so cool! +1 for spotting the appengine in the error message!
Daren Thomas
a u google-man ?
zjm1126
**google-man**? Is that mean "I am using google and answering you" OR "Am I from Google?" OR "google man pages"?
S.Mark
@S.Mark.Probably the second one.
SpawnCxy
Spawn, No, I am not :-)
S.Mark
Would be funny if Google was blocking itself :) (Check S.Mark's profile)
Tim Pietzcker
@Daren cough cough :D
badp
+3  A: 

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.
badp
If you want proof look at the stacktrace: `File "d:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1188, in __init__`
badp
A: 

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/

wescpy