[Tutor] unicode files

Yigal Duppen yduppen@xs4all.nl
Fri, 3 May 2002 18:13:02 +0200


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Steve,

> Is it a case of creating the file, writing to it and then saving it as
> unicode, or would I be better off writing the strings as unicode and then
> saving the file?

I'll come to that; however, from your question I got the feeling I'd better 
first explain a bit on file I/O in Python.

When you 'open'  a file for writing, a new file is created immediately; under 
the hood, this involves some interaction with the operating system where 
special resources are allocated; 

Then, whenever you call 'write', the text you write is _immediately_ written 
to the file. 

Finally, by calling 'close' you tell the operating system that all those 
special resources are no longer needed and that it can clean up your mess :). 
In other words, 'close'-ing a file is not at all similar to 'save'-ing a file 
like you do in a text-editor.

(Hopefully this also clarifies that when you read a file, you should call 
'close' as well -- when you read a file, special resources are allocated as 
well, and they too should be cleaned up).

As you can see, since Python does not support the concept of 'save'-ing a 
file, you can not save it as unicode. Therefore your only option is to write 
your strings as unicode.

(Note: in reality, 'write' does not always write your text to file 
immediately, due to a technique called buffering. 'close' also makes sure 
that everything is really really written to disk. So always call 'close' at 
the end)

> At the moment I have something like this:
>
> import sys
>
> starting_dir=raw_input("Dir and file please: ")
> somename=starting_dir
> file=open(somename, 'w')
> file.write("The first line of a file")
> file.close()

Writing unicode strings can be done as follows:

file.write(unicode("The first line of a file"))

or (if you write constant strings like in this example):

file.write(u"The first line of a file")

As I said, be sure to _always_ close the file, so you'd better do:


file = open(somename, 'w')
try:
	file.write("The first line of a file")
finally:
	file.close()


Hope this helps,

YDD
- -- 
.sigmentation Fault
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE80rcRLsKMuCf5EdwRAsJiAKCc3CnXcLnCmlH6yigGI3kJgwarRgCg+bEF
8ApNLgyKJQ/p1r3eO0q8TKo=
=OPMc
-----END PGP SIGNATURE-----