views:

267

answers:

2

Hi all, i'm having some trouble figuring out how to save unicode into a file in python. I have the following code, and if i run it in a script test.py, it should create a new file called priceinfo.txt, and write what's in price_info to the file. But i do not see the file, can anyone enlighten me on what could be the problem?

Thanks a lot!

price_info = u'it costs \u20ac 5'
f = codecs.open('priceinfo.txt','wb','utf-8')
f.write(price_info)
f.close()
+1  A: 

Assuming no error messages from the program (which would be the result of forgetting to import the codecs module), are you sure you're looking in the right place? That code writes priceinfo.txt in the current working directory (IOW are you sure that you're looking inside the working directory?)

oggy
You're right, unless someone is using windows explorer to run the script (and does not have a chance to see the output) or have a professional placed try... except around all the code :)
wuub
+3  A: 

I can think of several reasons:

  1. the file gets created, but in a different directory. Be certain what the working directory of the script is.
  2. you don't have permission to create the file, in the directory where you want to create it.
  3. you have some error in your Python script, and it does not get executed at all.

To find out which one it is, run the script in a command window, and check for any error output that you get.

Martin v. Löwis
+1, did not think of option #2
oggy
Option #2 would raise an IOError, which should be easily visible--and #3 should be raising an exception as well, so those should both be unlikely.I guess a particularly new Windows user might execute the script in a transient terminal window and never see the output.
Glenn Maynard
It does appear that for some reason it is creating the file in another directory. Perhaps I messed up something either in project plugin for vim or vim itself. Restarting the whole thing seem to work fine, thanks a lot!
FurtiveFelon