zoukankan      html  css  js  c++  java
  • 练习题1

    要求

    1. 输入一个目录
    2. 返回该文件夹中文件的路径
    3. 以及其包含文件夹中文件的路径

    实现

    import os
    import sys
    
    
    def print_directory_contents(sPath):
        # print os.listdir(sPath)  # 列出当前目录下的所有文件以及文件夹
        if os.path.exists(sPath):  # 首先判断目录是否存在
            for item in os.listdir(sPath):  # 循环当前目录下的每一个项目
                cPath = os.path.join(sPath, item)  # 将其与输入的路径进行拼接,组成完整路径
                # print currentPath
                if os.path.isdir(cPath):  # 如果是一个目录,那就把这个目录继续递归传入
                    print_directory_contents(cPath)
                else:
                    print cPath  # 直接打印文件路径
        else:
            exit("file or dir not exist")
    
    
    if __name__ == '__main__':
        if len(sys.argv) == 2:
            print_directory_contents(sys.argv[1])
        else:
            exit()
    
    

    技能点

    1. 递归
    2. 基础模块
    3. 系统交互
  • 相关阅读:
    第八章 多线程编程
    Linked List Cycle II
    Swap Nodes in Pairs
    Container With Most Water
    Best Time to Buy and Sell Stock III
    Best Time to Buy and Sell Stock II
    Linked List Cycle
    4Sum
    3Sum
    Integer to Roman
  • 原文地址:https://www.cnblogs.com/forsaken627/p/7597828.html
Copyright © 2011-2022 走看看