Python删除带有Unicode字符的文件

时间:2016-07-11 12:27:14

标签: python python-2.7 unicode python-unicode

我正在Python 2中编写一个清理方法,它应该删除任何文件,包括具有unicode名称的文件。 shutil.rmtree不是一个选项,因为它缺乏我需要的控制权。

目前,我尝试宣布# -*- coding: utf-8 -*-,然后使用.encode('utf-8').decode('utf-8')的组合,但没有成功。这就是我的方法在没有任何编码或解码的情况下的外观

def _remove_files(path):
    files = []

    for dirpath, dirnames, filenames in os.walk(path):
        for filename in [f for f in filenames]:
            files.append(os.path.join(dirpath, filename))

    for _file in files:
        print _file
        os.chmod(_file, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
        os.remove(_file)

这是我得到的错误:

  

WindowsError:[错误2]系统找不到指定的文件:u' C:\ Users \ xxxxx \ yyyyy \\ u65e5 \ u672c \ u8a9e \ u306e \ u656c \ u8a9e.txt'

0 个答案:

没有答案
相关问题