[Tutor] Puzzled again
Richard D. Moores
rdmoores at gmail.com
Wed Aug 3 17:50:28 CEST 2011
I wrote before that I had pasted the function (convertPath()) from my
initial post into mycalc.py because I had accidentally deleted it from
mycalc.py. And that there was no problem importing it from mycalc.
Well, I was mistaken (for a reason too tedious to go into). There WAS
a problem, the same one as before.
I now have a hex editor, HxD (<http://mh-nexus.de/en/programs.php>),
and have some info to report.
I've left convertPath() in mycalc.py, in importable form, and have
been experimenting with it in a new module, mycalc2.
The first run:
def convertPath(path):
"""
Given a path with backslashes, return that path with forward slashes.
By Steven D'Aprano 07/31/2011 on Tutor list
"""
#>>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
#>>> convertPath(path)
#'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf'
import os.path
separator = os.path.sep
if separator != '/':
path = path.replace(os.path.sep, '/')
return path
gets
C:\Windows\System32>python
Python 3.2.1 (default, Jul 10 2011, 20:02:51) [MSC v.1500 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mycalc2
>>>
Now I edit it back to its original problem form:
def convertPath(path):
"""
Given a path with backslashes, return that path with forward slashes.
By Steven D'Aprano 07/31/2011 on Tutor list
>>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
>>> convertPath(path)
'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf'
"""
import os.path
separator = os.path.sep
if separator != '/':
path = path.replace(os.path.sep, '/')
return path
and get
C:\Windows\System32>python
Python 3.2.1 (default, Jul 10 2011, 20:02:51) [MSC v.1500 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mycalc2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\site-packages\mycalc2.py", line 10
"""
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 144-146: truncated \UXXXXXXX
X escape
Using HxD, I find that the bytes in 144-146 are 20, 54, 75 or the
<space>, 'T', 'u' of " Tutor" . A screen shot of HxD with this
version of mycalc2.py open in it is at
<http://www.rcblue.com/images/HxD.jpg>. You can see that I believe the
offset integers are base-10 ints. I do hope that's correct, or I've
done a lot of work for naught.
Next I try
def convertPath(path):
"""
Given a path with backslashes, return that path with forward slashes.
By Steven D'Aprano 07/31/2011
>>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
>>> convertPath(path)
'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf'
"""
import os.path
separator = os.path.sep
if separator != '/':
path = path.replace(os.path.sep, '/')
return path
and get
>>> import mycalc2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\site-packages\mycalc2.py", line 10
"""
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 130-132: truncated \UXXXXXXX
escape
>>>
where 130-132 are 20, 30, 37 or the <space>, '0', '7' of " 07/31/2011".
So I go with
def convertPath(path):
"""
Given a path with backslashes, return that path with forward slashes.
By Steven D'Aprano
>>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
>>> convertPath(path)
'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf'
"""
import os.path
separator = os.path.sep
if separator != '/':
path = path.replace(os.path.sep, '/')
return path
and get
>>> import mycalc2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\site-packages\mycalc2.py", line 10
"""
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 118-120: truncated \UXXXXXXX
escape
Where 118-120 are 65, 6E, 20, or the 'e', 'n', <space> of "Steven "
I then delete that line, and go with
def convertPath(path):
"""
Given a path with backslashes, return that path with forward slashes.
>>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
>>> convertPath(path)
'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf'
"""
import os.path
separator = os.path.sep
if separator != '/':
path = path.replace(os.path.sep, '/')
return path
and get
>>> import mycalc2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\site-packages\mycalc2.py", line 9
"""
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 95-97: truncated \UXXXXXXXX
escape
where 95-97 are 64, 20, 73 or the 'd', <space>, 's' of "forward slashes"
So I delete "forward slashes" and go with
def convertPath(path):
"""
Given a path with backslashes, return that path with
>>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
>>> convertPath(path)
'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf'
"""
import os.path
separator = os.path.sep
if separator != '/':
path = path.replace(os.path.sep, '/')
return path
and get
>>> import mycalc2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\site-packages\mycalc2.py", line 9
"""
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 78-80: truncated \UXXXXXXXX
escape
where 78-80 are 20, 70, 61 or the <space>, 'p', 'a' of "that path".
I then delete that line, and the line above it, and go with
def convertPath(path):
"""
>>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
>>> convertPath(path)
'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf'
"""
import os.path
separator = os.path.sep
if separator != '/':
path = path.replace(os.path.sep, '/')
return path
and get
>>> import mycalc2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\site-packages\mycalc2.py", line 7
"""
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 20-22: truncated \UXXXXXXXX
escape
where 20-22 are 68, 29, 3A or the 'h', ')', ':' of "def convertPath(path):"
Obviously I can't delete those characters.
If you read this far, I thank you. I know it is tedious. But what in
the world is going on?
Steven asked if convertPath was a problem for Python 2.7. So I put
copied mycalc2.py to 2.7.1's site-packages folder and did:
def convertPath(path):
"""
Given a path with backslashes, return that path with forward slashes.
By Steven D'Aprano 07/31/2011 on Tutor list
"""
#>>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
#>>> convertPath(path)
#'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf'
import os.path
separator = os.path.sep
if separator != '/':
path = path.replace(os.path.sep, '/')
return path
Gets
C:\Windows\System32>C:\Python27\python
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mycalc2
>>>
And as he predicted,
def convertPath(path):
"""
Given a path with backslashes, return that path with forward slashes.
By Steven D'Aprano 07/31/2011 on Tutor list
>>> path = r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
>>> convertPath(path)
'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf'
"""
import os.path
separator = os.path.sep
if separator != '/':
path = path.replace(os.path.sep, '/')
return path
C:\Windows\System32>C:\Python27\python
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mycalc2
>>>
So no problems with convertPath() in 2.7.1 when imported.
Dick
More information about the Tutor
mailing list