Discuss / Python / 作业二

作业二

Topic source

Dong_锐华

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

def find_file(filePath, fileName):
  if not os.path.isabs(filePath):
    # 获取绝对路径
    filePath = os.path.abspath(filePath)
  for path in os.listdir(filePath):
    if os.path.isdir(os.path.join(filePath, path)):
      find_file(os.path.join(filePath, path), fileName)
    # 包含某个字符串
    elif fileName in path:
      # 获取从filePath到path的相对路径
      print(os.path.relpath(path, filePath))

find_file('.', 'Test.py')

  • 1

Reply