about special characters

Jianling Fan fanjianling at gmail.com
Sat Apr 30 14:13:51 EDT 2016


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!


On 29 April 2016 at 19:01, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, 30 Apr 2016 09:33 am, Jianling Fan wrote:
>
>> 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!
>
> 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
>
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Jianling Fan
樊建凌



More information about the Python-list mailing list