0

I want to copy the file to a new place in windows7 64bit. But I found some special characters in the file name can cause error 22 when using shutil.copy2 (but this file is legal in windows GUI and can be copied with a mouse). I believe the error22 means the source file can't be found by citing this file name. So my workaround would be to remove or replace the special characters in any file name which causes this problem in windows7 64bit. In general user keep copying files from other folders to this folder so the file name must be handled by some varables rather than constants. But as an example, I just put two files names in the example code. I put the code shown in the picture:

# -*- coding: gbk*-
#!/usr/bin/python
#Filename:ae.py
import os,shutil,time,re,string,sys  #re is regular expression
from nt import chdir
import win32api,win32file
import unicodedata,codecs
scr=r"C:\Users\Administrator\Desktop\« How-To Geek Forums.pdf"
des="C:\\Users\\Administrator\\Desktop\\How-To Geek Forums.pdf"
#chdir(os.path.dirname(scr))
os.rename(scr,des)

and I got

WindowsError:[Error 123]

I think that means I can't even rename it using python script once there are some special characters like «

Joe E
  • 1
  • 4
  • thanks kmario23, I had hard time editing to improve the fomat – Joe E Apr 02 '17 at 16:24
  • this question is about the workaround of the original question asked in another question [link](http://stackoverflow.com/questions/42804167/copy-files-with-strange-name-in-python-got-error-22). So if that can be solved, this question does not exist for this purpose – Joe E Apr 02 '17 at 16:25
  • Possible duplicate of [Remove all special characters, punctuation and spaces from string](http://stackoverflow.com/questions/5843518/remove-all-special-characters-punctuation-and-spaces-from-string) – wwii Apr 02 '17 at 16:30

1 Answers1

0

So, it looks like you are creating a regular expression to match the pattern of a filename, but you already know the name?

When you say special characters, are you speaking specifically about whitespace?

Here’s your code for readability:

#!/usr/bin/python
#Filename:ae.py 
import os,shutil,time,re,string,sys 
#re is regular expression 
from nt import chdir import win32api,win32file 
import unicodedata,codecs 
scr=r"C:\Users\Administrator\Desktop\« How-To Geek Forums.pdf" 
des="C:\Users\Administrator\Desktop\How-To Geek Forums.pdf" #
chdir(os.path.dirname(scr)) os.rename(scr,des)

check out http://docs.python.org/library/os.html#os.listdir

I would probably just get a list of files in the directory with

chdir(os.path.dirname(scr)
files = os.listdir(“.”)
for name in files:
    os.rename(name,des+”/“+name)
Aaron Paul
  • 117
  • 3
  • 14
  • You have some syntax problems, mainly the quotations – Taku Apr 02 '17 at 16:30
  • That python works on my Mac. I’m not sure what you mean? – Aaron Paul Apr 02 '17 at 16:53
  • thanks Aaron, "special characters" in this example is « in the original file name which caused the problem. There are many other examples, like the ´ in the file name d´caro groove - Mosa (Asi Voce Me Mata).krc I don't know the names of files in the source folder beforehand. I ignored the part that lists all the files in the source folder because there is no error in it. The part that causes error is the os.rename(scr,des) line in my script. I think the special character caused the problem. So don't worry about the listing part. – Joe E Apr 03 '17 at 01:27
  • Note the problem probably is harder than it appears. Simply replacing the special character won't work or is rather challenging. If one uses a black list of the special characters could cause this problem, she has to list all these special characters. Even one finds all this characters and replace them one by one using a regular expression for example, it probably won't work, because when I list() this file name, it shows « as ? in the ide and console. I am using python 2, maybe try 3? – Joe E Apr 03 '17 at 02:03
  • Do you really need to get rid of the special characters or are you just doing that as a work around for your copy problem? – Aaron Paul Apr 03 '17 at 02:08