开发者社区> 问答> 正文

打开python遍历中文目录得到的文件路径报错?报错

各位朋友们好,我是一名python新手,现在遇到一个问题,恳请各位朋友们能指点一下我。

我的环境是:ubuntu14.04系统,python2.7

用python遍历一个目录,目录层次结构如下:

这是主目录: /home/chaoma/superboard/mydata/input/production

主目录下的目录层次结构如下

--超级画板教学资源_全处理_772

                                 -- Z+Z资源库

                                                                 --初中数学资源库

                                                                                  --1有理数

                                                                                                       ---1.1数的分类

                                                                                                                 -- 0.mp4

                                                                 -- name.txt

                                                                                                                 ……

                                                                                       --2一元一次方程

                                                                                       …..

                                               --动态解析高考数学综合题

                                                                     --第一章基本函数

                                                                                           --第四节

                                                                                                          --06福建理21

                                                                                                                             --name.txt

                                                                                                                                   --0.mp4

                                                                                                          --拓展练习4.1

                                                        ….

                                                                                                --第五节

                                                                                                ……

                                                                           --第二章三角函数

                                                                           …….

                                                ……

--学生作品集合_全处理_52

                     ………

                                                                                                                                                                   

我想实现的功能:找到所有含有0.mp4文件的父目录,并读取该父目录下面的name.txt文件

因此我写了以下的遍历函数来实现这一功能:

def visitDir_walk(path , sFileSuffix):

    codedetect = chardet.detect(path)["encoding"]

    path = unicode(path , codedetect)

    fileNames = list()

    for root,dirs,files in os.walk(path):

        for i in range(0 , len(files)):

            file = files[i]

            if file.endswith(sFileSuffix):

                sFileName =  os.path.join(root , file)

#这边返回的应该是包含视频文件的父目录,也就是学生作品目录

                realFileName = os.path.split(os.path.abspath(sFileName))[0]

                fileNames.append( unicode(realFileName,”gbk”) )

return fileNames

 

def main():

         sProductionInputPath = “/home/chaoma/superboard/mydata/input/production”

         dir_production_list  =  visitDir_walk(sProductionInputPath , ".mp4")

#拼接name.txt的路径

for dir in dir_production_list:

nameFile = os.path.join(dir , "name.txt")

fout = codecs.open(nameFile , "w" , "utf-8")

#截取出路径中的文件名作为需要的内容,并写入文件

fout.write(os.path.split(path)[1])                       

fout.close()

main()

 

 

但是执行之后却报错了,报错如下:

    fout = codecs.open(nameFile , "w" , "utf-8")

  File "/usr/lib/python2.7/codecs.py", line 878, in open

    file = __builtin__.open(filename, mode, buffering)

IOError: [Errno 2] No such file or directory: u'/home/chaoma/superboard/mydata/input/production/\u8d85\u7ea7\u753b\u677f\u6559\u5b66\u8d44\u6e90_\u5168\u5904\u7406_772/\u52a8\u6001\u89e3\u6790\u9ad8\u8003\u6570\u5b66\u7efc\u5408\u9898/\u7b2c\u4e00\u7ae0\u57fa\u672c\u51fd\u6570/\u7b2c\u56db\u8282/06\u798f\u5efa\u740621/name.txt'

打印这个报错的路径得到:

/home/chaoma/superboard/mydata/input/production/超级画板教学资源_全处理_772/动态解析高考数学综合题/第一章基本函数/第四节/06福建理21/name.txt

这个文件路径是存在的却报错,不知道为什么。

恳请各位朋友们能指点我一下,十分感谢。

展开
收起
爱吃鱼的程序员 2020-06-14 14:42:49 671 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    你没看到报错的路径最后的汉字都变成码了吗,这说明路径中有汉字的你需要再做一些处理!

    python对汉字不是支持的很好,你可以查一下具体的资料!

    你open文件的时候,utf-8这种编码格式是不支持汉字的你

    2020-06-14 14:43:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载