Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107870 > unrolled thread
| Started by | Jianling Fan <fanjianling@gmail.com> |
|---|---|
| First post | 2016-04-29 17:33 -0600 |
| Last post | 2016-04-30 13:48 -0600 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Jianling Fan <fanjianling@gmail.com> |
|---|---|
| Date | 2016-04-29 17:33 -0600 |
| Subject | about special characters |
| Message-ID | <mailman.234.1461973268.32212.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-04-30 11:01 +1000 |
| Message-ID | <572403de$0$1601$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #107870 |
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
[toc] | [prev] | [next] | [standalone]
| From | Jianling Fan <fanjianling@gmail.com> |
|---|---|
| Date | 2016-04-30 12:13 -0600 |
| Message-ID | <mailman.267.1462040034.32212.python-list@python.org> |
| In reply to | #107876 |
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@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
樊建凌
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2016-04-30 15:36 -0400 |
| Message-ID | <mailman.268.1462044980.32212.python-list@python.org> |
| In reply to | #107876 |
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
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-04-30 20:42 +0100 |
| Message-ID | <mailman.269.1462045349.32212.python-list@python.org> |
| In reply to | #107876 |
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'
[toc] | [prev] | [next] | [standalone]
| From | Jianling Fan <fanjianling@gmail.com> |
|---|---|
| Date | 2016-04-30 13:48 -0600 |
| Message-ID | <mailman.270.1462045688.32212.python-list@python.org> |
| In reply to | #107876 |
Oh, it works!
This is the simplest and best way!
Thanks very much!
On 30 April 2016 at 13:42, MRAB <python@mrabarnett.plus.com> wrote:
> 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'
>
> --
> https://mail.python.org/mailman/listinfo/python-list
--
Jianling Fan
樊建凌
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web