Discuss / Python / 第一和第二

第一和第二

Topic source

tycoonBrain

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

#利用os模块编写一个能实现dir -1输出的程序。

import os

path =  input('输入你的绝对路径,帮你降一级目录:')

print(os.path.split(path)[0])

#or

print(os.path.abspath(r'..'))

#编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。

import os

def search_file(path,str):

    for i in os.listdir(path):

        if os.path.isfile(i):

            if str in i:

                print(os.path.join(path,i))

        else:

            search_file(os.path.join(path,i),str)

search_file(os.path.abspath('.'),'te')

tycoonBrain

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

原来第一题理解错了......哈哈哈


  • 1

Reply