宝安营销型网站建设公司seo研究院
通过python可以给定一个搜索路径(一个描述目录信息的字符串),需要根据这个路径和请求的文件名找到第一个符合要求的文件
具体代码如下:
import os
def search_file(filename,search_path,pathsep=os.pathsep):
""" 给定一个搜索路径,根据请求的名字找到文件 """
for path in search_path.split(pathsep):
candidate = os.path.join(path,filename)
if os.path.isfile(candidate):
return os.path.abspath(candidate)
return None
if __name__ == '__main__':
search_path = '/bin' + os.pathsep + '/usr/bin' # ; on Windows, :on UNIX
find_file = search_file('ls', search_path)
if find_file:
print "File 'ls' found at %s" % find_file
else:
print "File 'ls' not found"
用python编译以后,发现ls这个字符串在的路径在
File 'ls' found at /bin/ls