码迷,mamicode.com
首页 > 编程语言 > 详细

python实现linux下文件遍历

时间:2019-03-02 10:49:46      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:mes   inux   end   ring   pop   split   文件   linu   []   

import os

def getAllFile(*names):
    if len(names) == 0:
        return ""
    else:
        allList = []
        for name in names:
            fileAndDirString = os.popen("ls -l "+name).read()
            fileAndDirStringDo = "\n".join(fileAndDirString.split("\n")[1:-1])
            if fileAndDirStringDo:
                fileAndDirList = [fileOrDir.split(" ")[0]+" "+name+"/"+fileOrDir.split(" ")[-1] for fileOrDir in fileAndDirStringDo.split("\n")]
                allList.extend(fileAndDirList)
            # print(allList)
        fileList = []
        dirList = []

        for fileAndDir in allList:
            # print(fileAndDir)
            if fileAndDir[0] == "-":
                fileList.append(fileAndDir.split(" ")[-1])
            else:
                dirList.append(fileAndDir.split(" ")[-1])

        return "\n".join(fileList)+"\n"+getAllFile(*dirList)


r = getAllFile("/home/taomin")
print(r)

 

python实现linux下文件遍历

标签:mes   inux   end   ring   pop   split   文件   linu   []   

原文地址:https://www.cnblogs.com/taomin/p/10459528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!