Discuss / Python / 第二题弄出来了,但是感觉代码写的好乱好复杂……

第二题弄出来了,但是感觉代码写的好乱好复杂……

Topic source

_周思思_

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

第二题弄出来了,但是感觉代码写的好乱好复杂…… 总觉得在走绕路……

import os


def print_filename(file_path):
    os.chdir(file_path)  # 改变目录到指定目录
    for x in os.listdir('.'):
        if os.path.isfile(x) and os.path.splitext(x)[1] == '.py':
            # file_path[len(full_path)+1:] 截取当前文件夹的相对路径
            if len(file_path[len(full_path)+1:]) == 0:
                print(x)
            else:
                print(file_path[len(full_path)+1:], ' ==> ', x)


if __name__ == '__main__':
    # 获取当前目录的绝对路径
    full_path = os.path.abspath('.')

    # 遍历目录(包括子目录)
    for path in os.walk(full_path):
        print_filename(path[0])  # path[0] 当前遍历目录的路径

_周思思_

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

好像审题审错啦…… 哈哈哈……


  • 1

Reply