Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

about special characters

41 views
Skip to first unread message

Jianling Fan

unread,
Apr 29, 2016, 7:41:19 PM4/29/16
to
Hello everyone,

I am trying to use python 27 copying some of my folders and files to
another directory.
My code works good for other files but I have some problem to copy
files that have some special characters in the filename. like
filenames contain Greek "δ" or latin "š".
it always gave a error that "No such file or directory:"

Any help will be appreciate!
Thanks!

Regards,

Jianling

Ben Finney

unread,
Apr 29, 2016, 7:52:51 PM4/29/16
to
Jianling Fan <fanji...@gmail.com> writes:

> I am trying to use python 27 copying some of my folders and files to
> another directory.

(There has never been a Python 27. I assume Python 2.7 is what you meant.)

> My code works good for other files but I have some problem to copy
> files that have some special characters in the filename. like
> filenames contain Greek "δ" or latin "š".

You may already know that Python 2 handles international text a lot less
consistently than Python 3.

Is it at all feasible for you to use Python 3 instead? Text handling is
specifically a big advantage of Python 3.

> it always gave a error that "No such file or directory:"

Please give a complete, small example:

* The actual filenames.

* The exact filesystem encoding that filesystem uses. Be sure to verify that!

* A very small, but complete, Python program which exhibits the
behaviour. We will need to be able to run the same program.

* The complete error output (the full traceback).

--
\ “Corporation, n. An ingenious device for obtaining individual |
`\ profit without individual responsibility.” —Ambrose Bierce, |
_o__) _The Devil's Dictionary_, 1906 |
Ben Finney

MRAB

unread,
Apr 29, 2016, 8:33:01 PM4/29/16
to
On 2016-04-30 00:52, Ben Finney wrote:
> Jianling Fan <fanji...@gmail.com> writes:
>
>> I am trying to use python 27 copying some of my folders and files to
>> another directory.
>
> (There has never been a Python 27. I assume Python 2.7 is what you meant.)
>
>> My code works good for other files but I have some problem to copy
>> files that have some special characters in the filename. like
>> filenames contain Greek "δ" or latin "š".
>
> You may already know that Python 2 handles international text a lot less
> consistently than Python 3.
>
> Is it at all feasible for you to use Python 3 instead? Text handling is
> specifically a big advantage of Python 3.
>
>> it always gave a error that "No such file or directory:"
>
> Please give a complete, small example:
>
> * The actual filenames.
>
> * The exact filesystem encoding that filesystem uses. Be sure to verify that!
>
> * A very small, but complete, Python program which exhibits the
> behaviour. We will need to be able to run the same program.
>
> * The complete error output (the full traceback).
>
It might be a Unicode problem, i.e. trying to work with bytestrings
(str) instead of Unicode strings (unicode) and using the wrong encoding.

Steven D'Aprano

unread,
Apr 29, 2016, 8:57:10 PM4/29/16
to
On Sat, 30 Apr 2016 09:52 am, Ben Finney wrote:

> Jianling Fan <fanji...@gmail.com> writes:
>
>> I am trying to use python 27 copying some of my folders and files to
>> another directory.
>
> (There has never been a Python 27. I assume Python 2.7 is what you meant.)

I believe that Python X.Y shows up as "PythonXY" under Windows.



--
Steven

Steven D'Aprano

unread,
Apr 29, 2016, 9:01:30 PM4/29/16
to
Please put yourself in our shoes. Read your message above, and, imagine that
you know NOTHING else about your program than what you write above. What
answer would you give?

Please tell us what you have actually done. How do you copy the files? How
do you enter the file names? What OS are you using?



--
Steven

Ben Finney

unread,
Apr 29, 2016, 9:49:31 PM4/29/16
to
Then that's a bug which should be fixed, IMO. An MS Windows user (i.e.,
not me) would be well placed to describe the behaviour in a bug report.

--
\ “Most people, I think, don't even know what a rootkit is, so |
`\ why should they care about it?” —Thomas Hesse, Sony BMG, 2006 |
_o__) |
Ben Finney

eryk sun

unread,
Apr 29, 2016, 10:36:36 PM4/29/16
to
On Fri, Apr 29, 2016 at 8:48 PM, Ben Finney <ben+p...@benfinney.id.au> wrote:
> Steven D'Aprano <st...@pearwood.info> writes:
> Then that's a bug which should be fixed, IMO. An MS Windows user (i.e.,
> not me) would be well placed to describe the behaviour in a bug report.

It's Python X.Y in the start menu and Python X.Y.Z in sys.version. The
default installation directory is PythonXY (for 3.5.0 it was "Python
3.5"; this was reverted in 3.5.1), and the DLL is pythonXY.dll. The
executables are python.exe and pythonw.exe.

Terry Reedy

unread,
Apr 29, 2016, 10:37:59 PM4/29/16
to
On 4/29/2016 9:48 PM, Ben Finney wrote:
> Steven D'Aprano <st...@pearwood.info> writes:
>
As a directory name that also omits the space, yes. Start Menu entries
have both the space and and the '.'.

> Then that's a bug which should be fixed, IMO. An MS Windows user (i.e.,
> not me) would be well placed to describe the behaviour in a bug report.

I disagree. .0, .1, etc, would look like extensions, but are not.


--
Terry Jan Reedy

Ben Finney

unread,
Apr 29, 2016, 10:43:58 PM4/29/16
to
Terry Reedy <tjr...@udel.edu> writes:

> On 4/29/2016 9:48 PM, Ben Finney wrote:
> > Steven D'Aprano <st...@pearwood.info> writes:
> >
> As a directory name that also omits the space, yes. Start Menu entries
> have both the space and and the '.'.

Okay. So as you describe the behaviour on MS Windows, it doesn't support
the OP's choice of saying “python 27” to specify Python 2.7 — MS Windows
appears to be irrelevant to that.

--
\ “Computer perspective on Moore's Law: Human effort becomes |
`\ twice as expensive roughly every two years.” —anonymous |
_o__) |
Ben Finney

MRAB

unread,
Apr 29, 2016, 10:57:02 PM4/29/16
to
On 2016-04-30 03:35, eryk sun wrote:
> On Fri, Apr 29, 2016 at 8:48 PM, Ben Finney <ben+p...@benfinney.id.au> wrote:
>> Steven D'Aprano <st...@pearwood.info> writes:
>> Then that's a bug which should be fixed, IMO. An MS Windows user (i.e.,
>> not me) would be well placed to describe the behaviour in a bug report.
>
> It's Python X.Y in the start menu and Python X.Y.Z in sys.version. The
> default installation directory is PythonXY (for 3.5.0 it was "Python
> 3.5"; this was reverted in 3.5.1), and the DLL is pythonXY.dll. The
> executables are python.exe and pythonw.exe.
>
There's an option in Windows to hide the extension. I wondered whether
that had something to do with it.

It turns out that it applies only to file names, not folder names, so
you can have multiple files apparently with the same name, but folder
names remain distinct.

Jianling Fan

unread,
Apr 30, 2016, 2:14:08 PM4/30/16
to
Hello everyone,

Thanks very much for all your replies and sorry for the inconvience.
This is my first time to post question in this list.

I am using python 2.7 in Windows 7 Enterprise version.

Here is the the filename that cause the problem: "Decock-2013-On the
potential of δ18O and δ15N.pdf"
When I delete the "δ" in the filename, the script works good.

Here is the output:

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> runfile('P:/sync.py', wdir='P:')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py",
line 699, in runfile
execfile(filename, namespace)
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py",
line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "P:/sync.py", line 50, in <module>
sync_files(src, dest)
File "P:/sync.py", line 43, in sync_files
sync(dir_cmp)
File "P:/sync.py", line 20, in sync
shutil.rmtree(f_right)
File "C:\Python27\lib\shutil.py", line 236, in rmtree
onerror(os.listdir, path, sys.exc_info())
File "C:\Python27\lib\shutil.py", line 234, in rmtree
names = os.listdir(path)
WindowsError: [Error 3] The system cannot find the path specified:
'P:/mystuff\\Decock-2013-On the potential of d18O and d15N.pdf/*.*'


Here is my code to do this work: I am using this script to sync my
files between different disks.

#coding=utf-8

import filecmp, shutil, os, sys

SRC = r'C:/Disk/mystuff'
DEST = r'P:/mystuff'

IGNORE = ['Thumbs.db']

def get_cmp_paths(dir_cmp, filenames):
return ((os.path.join(dir_cmp.left, f),
os.path.join(dir_cmp.right, f)) for f in filenames)

def sync(dir_cmp):
for f_left, f_right in get_cmp_paths(dir_cmp, dir_cmp.right_only):
if os.path.isfile(f_right):
os.remove(f_right)
else:
shutil.rmtree(f_right)
print('delete %s' % f_right)
for f_left, f_right in get_cmp_paths(dir_cmp,
dir_cmp.left_only+dir_cmp.diff_files):
if os.path.isfile(f_left):
shutil.copy2(f_left, f_right)
else:
shutil.copytree(f_left, f_right)
print('copy %s' % f_left)
for sub_cmp_dir in dir_cmp.subdirs.values():
sync(sub_cmp_dir)

def sync_files(src, dest, ignore=IGNORE):
if not os.path.exists(src):
print('= =b Please check the source directory was exist')
print('- -b Sync file failure !!!')
return
if os.path.isfile(src):
print('#_# We only support for sync directory but not a single
file,one file please do it by yourself')
print('- -b Sync file failure !!!')
return
if not os.path.exists(dest):
os.makedirs(dest)
dir_cmp = filecmp.dircmp(src, dest, ignore=IGNORE)
sync(dir_cmp)
print('^_^ Sync file finished!')

if __name__ == '__main__':
src, dest = SRC, DEST
if len(sys.argv) == 3:
src, dest = sys.argv[1:3]
sync_files(src, dest)



Thanks again!
> --
> https://mail.python.org/mailman/listinfo/python-list



--
Jianling Fan
樊建凌

Terry Reedy

unread,
Apr 30, 2016, 3:36:35 PM4/30/16
to
On 4/30/2016 2:13 PM, Jianling Fan wrote:

> I am using python 2.7 in Windows 7 Enterprise version.
>
> Here is the the filename that cause the problem: "Decock-2013-On the
> potential of δ18O and δ15N.pdf"
> When I delete the "δ" in the filename, the script works good.

You may be able to get "δ" (and other Greek characters) to work on your
system with 2.7, but if you want to be able to work with any filename on
Windows, get Python 3.4 or later and use text strings, not byte strings.
You may someday run into a Name-your-title.pdf file name with other
'strange' characters from other

Windows filenames are unicode strings and are stored, I believe, with
utf-16 encoding. Even in early 3.x versions, there were Windows path
problems, for instance with east Asian characters. Multiple 3.x patches
seem to have fixed the problems. So merely using unicode is not, in
general, sufficient.

--
Terry Jan Reedy


MRAB

unread,
Apr 30, 2016, 3:42:42 PM4/30/16
to
On 2016-04-30 19:13, Jianling Fan wrote:
> Hello everyone,
>
> Thanks very much for all your replies and sorry for the inconvience.
> This is my first time to post question in this list.
>
> I am using python 2.7 in Windows 7 Enterprise version.
>
> Here is the the filename that cause the problem: "Decock-2013-On the
> potential of δ18O and δ15N.pdf"
> When I delete the "δ" in the filename, the script works good.
>
[snip]
You're using bytestrings (str). As soon as you step out of the ASCII
range, you can face problems with which encoding it's using.

The simplest fix is to switch to using Unicode.

Start with this change:

SRC = ur'C:/Disk/mystuff'
DEST = ur'P:/mystuff'

Jianling Fan

unread,
Apr 30, 2016, 3:48:25 PM4/30/16
to
Oh, it works!

This is the simplest and best way!

Thanks very much!



On 30 April 2016 at 13:42, MRAB <pyt...@mrabarnett.plus.com> wrote:
> On 2016-04-30 19:13, Jianling Fan wrote:
>>
>> Hello everyone,
>>
0 new messages