1
回答
打开python遍历中文目录得到的文件路径报错
终于搞明白,存储TCO原来是这样算的>>>   

各位朋友们好,我是一名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

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

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

举报
qingyuanlu
发帖于2年前 1回/851阅
顶部