Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107922

Re: about special characters

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: about special characters
Date 2016-04-30 20:42 +0100
Message-ID <mailman.269.1462045349.32212.python-list@python.org> (permalink)
References <CAJ7mry+o=PRGtM46HPGdNVqaowy3tmheJKu38nRy4y_QEjqV-w@mail.gmail.com> <mailman.234.1461973268.32212.python-list@python.org> <572403de$0$1601$c3e8da3$5496439d@news.astraweb.com> <CAJ7mryKF5N4crnz2Lu36JC=LiqCKYCtwjgoV4U_=h4=ovN6Q6w@mail.gmail.com> <142f82f4-b266-3b85-f606-e1b6d3246021@mrabarnett.plus.com>

Show all headers | View raw


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.
>
> 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)
>
[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'

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

about special characters Jianling Fan <fanjianling@gmail.com> - 2016-04-29 17:33 -0600
  Re: about special characters Steven D'Aprano <steve@pearwood.info> - 2016-04-30 11:01 +1000
    Re: about special characters Jianling Fan <fanjianling@gmail.com> - 2016-04-30 12:13 -0600
    Re: about special characters Terry Reedy <tjreedy@udel.edu> - 2016-04-30 15:36 -0400
    Re: about special characters MRAB <python@mrabarnett.plus.com> - 2016-04-30 20:42 +0100
    Re: about special characters Jianling Fan <fanjianling@gmail.com> - 2016-04-30 13:48 -0600

csiph-web