Discuss / Python / 简单易懂

简单易懂

Topic source

Pop_林肥強

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

 # 利用os模块编写一个能实现dir -l输出的程序
 if input() == 'dir -l':
     curr_files = [x for x in os.listdir('.')]
     print(curr_files)


 # 编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
 def get_file_paths_by_key(path, key):
     for x in os.listdir(path):
         x_path = os.path.join(path, x)
         if os.path.isfile(x_path):
             if key in os.path.split(x_path)[1]:
                 print(os.path.relpath(x_path))
         elif os.path.isdir(x_path):
             get_file_paths_by_key(x_path, key)


 get_file_paths_by_key('.', '.py')

  • 1

Reply