Discuss / Python / 作业_操作文件和目录

作业_操作文件和目录

Topic source

落之萧萧

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

参考了一下其他人的作业,最后自己整理了这个:

import osdef myfile(dirname, key):    global ans_file    for filename in os.listdir(dirname):        path = os.path.join(dirname, filename)        if os.path.isdir(path):            try:                myfile(path, key)            except BaseException as e:                print(e)        if os.path.splitext(path)[1] == key:            ans_file.append(path)    return ans_fileif __name__ == '__main__':    ans_file = []    myfile('\Tools\python37', '.py')    for x in ans_file:        if os.path.split(x):            print(os.path.split(x))

  • 1

Reply