简体   繁体   中英

Delete folders which contains special chars in Python

I have some folders which names contains special chars. I tried to remove them like this:

shutil.rmtree(os.path.join(mypath,"Input"))

and I get an error:

The filename, directory name, or volume label syntax is incorrect: ...\\library elements ???? ?????

and my Folder name is :

library elements 階段要素 Элементы

How can I delete this folder?

Thanks David

Assuming you're on Python 2.x, this appears to be a known bug in Python 2.7 . It's likely a problem with locale handling when using the ANSI APIs on Windows (Unicode and locale handling was much weaker in the 2.x line).

Assuming you can't switch to Python 3.x, try passing the path as a unicode path, rather than str , so it exercises the Windows Unicode APIs (that will handle the non-ASCII names correctly). Since your base path appears to be an ASCII str , this can be done by changing:

shutil.rmtree(os.path.join(mypath,"Input"))

to:

shutil.rmtree(os.path.join(mypath,"Input").decode('ascii'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM