about special characters

MRAB python at mrabarnett.plus.com
Sat Apr 30 15:42:16 EDT 2016


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'




More information about the Python-list mailing list