Re: [Tutor] os.rename anomaly in Python 2.3 on Windows XP

2007-10-10 Thread Terry Carroll
On Tue, 9 Oct 2007, Tony Cappellini wrote:

 Unfortunately,os.listdir() returns the same string as glob.glob, for
 the problem file I mentioned.

Tony --

Try specifying the argument to os.listdir as a unicode string.  I've found
that cures many ailments like this.

e.g., instead of something like:

filelist = os.listdir('.')

use:

filelist = os.listdir(u'.')

I don't think glob supports this.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] os.rename anomaly in Python 2.3 on Windows XP

2007-10-09 Thread Tim Golden
Tony Cappellini wrote:
 Using Windows XP, SP2 and Python 2.3
 
 I've written a script which walks through a bunch of directories and
 replaces characters which are typically illegals as filenames, with an
 '_' character.

[...]

 When my script encounters a directory with the unwanted characters,
 it's easy to detect them and filter them out. The next step is to
 rename the file to get rid of the problem characters.

[...]

 However, recently when I called os.rename(oldname, newname) an OS
 exception was thrown with Illegal filename. I was able to narrow it
 down to oldname being the cause of the problem.
 Some of the characters showed up as ? in the Python strings.
 
 Oddly enough, os.rename() cannot perform the renaming of the
 directories, but I can do this manually in File Explorer or even in a
 CMD console using rename
 
 So what is os.renaming() actually calling on a Windows system, that
 won't allow me to rename dirs with illegal characters?


Well, the simple answer to that is (cut-and-pasted and snipped a bit)
from the posixmodule.c source:

if (unicode_file_names()) {
...
result = MoveFileW(PyUnicode_AsUnicode(o1),
PyUnicode_AsUnicode(o2));
...
result = MoveFileA(p1, p2);


so it's using the MoveFileW with two unicode filenames, or
the MoveFileA with two non-unicode filenames. So... are you
calling os.rename with unicode or non-unicode filenames?

If you're using, say, os.walk or os.listdir to walk your tree,
pass it a unicode path to start with, and the filenames coming
back will also be unicode. Try this, for example:

code
import os, sys

#
# filename with random non-ascii char
#
filename = uabc\u0123.txt
open (filename, w).close ()

for i in os.listdir (u.):
   print i.encode (sys.stdout.encoding, replace)

new_filename = unicode (filename.encode (ascii, replace).replace (?, _))
os.rename (filename, new_filename)

for i in os.listdir (u.):
   print i

/code

The filename with the random unicode char is
shown (with the fill-in question-mark) in the
initial list. It's then renamed with the non-ascii
char replaced by _ and appears without an encoding
in the final list.

I think this is what you're after.

TJG
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] os.rename anomaly in Python 2.3 on Windows XP

2007-10-09 Thread Tony Cappellini
Thanks.

Unfortunately,os.listdir() returns the same string as glob.glob, for
the problem file I mentioned.
When I pass that string to os.rename()

OSError: [Errno 22] Invalid argument

 Sounds like it has something to do with Unicode.
 Your filenames aren't being interpreted correctly.  Perhaps os.listdir
 is giving you the UTF-8 versions
 rather than the Unicode versions of the filenames?
 -Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] os.rename anomaly in Python 2.3 on Windows XP

2007-10-08 Thread Luke Paireepinart
Tony Cappellini wrote:
 Using Windows XP, SP2 and Python 2.3

 I've written a script which walks through a bunch of directories and
 replaces characters which are typically illegals as filenames, with an
 '_' character.

 The directories are part of a package of software which is released by
 a group of people from Japan, and as such, they use their own
 character set (probably Kanji). However, most of the time, there are
 only 1 or 2 directories with unknown or illegal characters, as
 determined by
 my system (which does not use the Kanji characters).

 When my script encounters a directory with the unwanted characters,
 it's easy to detect them and filter them out. The next step is to
 rename the file to get rid of the problem characters.

 However, recently when I called os.rename(oldname, newname) an OS
 exception was thrown with Illegal filename. I was able to narrow it
 down to oldname being the cause of the problem.
 Some of the characters showed up as ? in the Python strings.

 Oddly enough, os.rename() cannot perform the renaming of the
 directories, but I can do this manually in File Explorer or even in a
 CMD console using rename

 So what is os.renaming() actually calling on a Windows system, that
 won't allow me to rename dirs with illegal characters?
   
Sounds like it has something to do with Unicode.
Your filenames aren't being interpreted correctly.  Perhaps os.listdir 
is giving you the UTF-8 versions
rather than the Unicode versions of the filenames?
-Luke
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor