[Tutor] Non-ASCII

Shawn Matlock matlocs at odscompanies.com
Fri Oct 8 17:32:18 CEST 2010


You're guess was right, copying the code from the email message worked. My IDE - Eclipse/PyDev - must have been hiding the control character. I had tried copying my code out to a text editor and back before asking for help, but it didn't work. Perhaps I didn't "select all" before pasting the code back.

Thank you for the explanation.


-----Original Message-----
From: Evert Rol [mailto:evert.rol at gmail.com] 
Sent: Thursday, October 07, 2010 12:34 PM
To: Shawn Matlock
Cc: tutor at python.org
Subject: Re: [Tutor] Non-ASCII

> I'm going through an online tutorial for Jython (www.jython.org). I can't find a place to ask a question on that site so I thought I'd try here. I believe the code is supposed to traverse a directory, identifying file types. The script is failing with the following message:
>  File "<string>", line None
> SyntaxError: Non-ASCII character in file 'H:\workspace\test\src\root\nested\example.py', but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Normally, this (obviously) means you have some non-ASCII characters (accented characters would already do this) in your source code. Since this is just a byte code at the lowest level, it needs to be decoded into something sensible, which you specify using an encoding. Since Python refuses to guess which encoding you want to use (the same character code could result in totally different characters for different encodings), it's bailing out instead of continuing (the exception being that by default, the Python interpreter assumes your encoding is ASCII, and happily proceeds with that until it comes across a character code outside the ASCII range).

In your code below, however, I don't see any non-ASCII characters. So my best guess is that there is some invisible control-character outside the ASCII range that's causing this error message. 
This could happen because of a particular type of text-editor/IDE you're using.

The odd thing I find about the SyntaxError is actually that there is no line number mentioned. In fact, 'File "<string>", line None' is a very weird indication for telling where the error occurred. Might again be something to do with your IDE.

I've tried your code by simply copy-pasting, and that works fine for me. So the control-character didn't reach my mail program. In which case you could try to copy-past the code from this reply into a new file and see if that's gotten rid of the control-character (presuming that caused the problem).

Of course, if it's eg a known problem in Jython, I can't really tell you, because I simply don't know Jython. But try clean code for a start.



> Thank you,
> Shawn

import os, sys 
from stat import *

def walktree(top, callback):
   '''recursively descend the directory tree rooted at top,
   calling the callback function for each regular file'''
   for f in os.listdir(top):
       pathname = os.path.join(top, f) 
       mode = os.stat(pathname)[ST_MODE] 
       if S_ISDIR(mode):
           # It's a directory, recurse into it 
           walktree(pathname, callback)
       elif S_ISREG(mode):
           # It's a file, call the callback function 
           # Unknown file type, print a message 
           print 'Skipping %s' % pathname

def visitfile(file):
   print 'visiting', file

if __name__ == '__main__':
   walktree(sys.argv[1], visitfile)

> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list