Discuss / Python / 题目二用walk 。题目一不太会,用楼下仁兄的

题目二用walk 。题目一不太会,用楼下仁兄的

Topic source

小羊飞的

#1 Created at ... [Delete] [Delete and Lock User]

题目二: import os import sys

def has_str(dir,str):

root_dir = os.walk(dir)
for path,d,filelist in root_dir:
    for filename in filelist:
        if str in os.path.splitext(filename)[0]:
            print(filename)
            print(path)

has_str(sys.argv[1],sys.argv[2])

题目一:

import os import time class MyLsL(object): def init(self,dir='.'): self.dirList=os.listdir(dir)

def ls(self):
    fileLists=[]
    for file in self.dirList:
        print(self.getFileInfo(file))


def getFileInfo(self,fileName):
    size = str(os.path.getsize(fileName))
    fileType = 'd' if os.path.isdir(fileName) else '-'
    readAble = 'r' if os.access(fileName,os.R_OK) else '-'
    writeAble = 'w' if os.access(fileName,os.W_OK) else '-'
    opeAble = 'x' if os.access(fileName,os.X_OK) else '-'
    timestamp = os.path.getctime(fileName)
    timeLocal = time.localtime(timestamp)
    dt = time.strftime('%Y-%m-%d',timeLocal)
    return fileType+readAble+writeAble+' '+opeAble+' '+size+' '+dt

  • 1

Reply