Discuss / Python / 自测成功,只需输入需要查询的目录,通过可变参数判断是否为递归的首次来要求输入文件名

自测成功,只需输入需要查询的目录,通过可变参数判断是否为递归的首次来要求输入文件名

Topic source

Exmlyshy

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

通过可变参数判断是否为递归首次来要求输入文件名

<pre> import os def my_find(*s):#可变参数满足递归 #os.chdir(path)更改当前路径为path path=s[0] global relpath if len(s)>1: relpath=s[1] key=input('请输入要查询的文件名:\n') for x in os.listdir(path): if os.path.isfile(path+'\'+x) and os.path.splitext(x)[1]=='.py': print(x) print('relpath:%s'%os.path.relpath(path,relpath))#os.path.relpath(path[,start])获得path相对于start的相对路径 if os.path.isdir(path+'\'+x): my_find(path+'\'+x) path=input('请输入要查询的目录路径:\n') my_find(path,path) </pre> 输出 <pre> 请输入要查询的目录路径: E:\python 请输入要查询的文件名: .py 1.py relpath:day01 1.py relpath:day01\day 2.py relpath:day02 default_para.py relpath:day02 deftest.py relpath:day02 exercise.py relpath:day02 hanoi.py relpath:day02 variablepara.py relpath:day02 FindMinAndMax.py relpath:day03 List_lower.py relpath:day03 triangles.py relpath:day03 trim.py relpath:day03 normalize.py relpath:day04 prod.py relpath:day04 str2float.py relpath:day04 is_palindrome.py relpath:day05 sorted.py relpath:day05 createCounter.py relpath:day06 decorator.py relpath:day06 lambda.py relpath:day06 hello.py relpath:day07 hello.py relpath:day07\mymodule init.py relpath:day07\mymodule class.py relpath:day08 student.py relpath:day08 @property.py relpath:day09 enum.py relpath:day09 err.py relpath:day10 fact2.py relpath:day10 mydict.py relpath:day10 mydict2.py relpath:day10 mydict_test.py relpath:day10 student_test.py relpath:day10 my_find.py relpath:day11 open.py relpath:day11 test.py relpath:. </pre>


  • 1

Reply