From:Joseph
[Python] 纯文本查看 复制代码 #! /usr/bin/python
# -*- coding: utf-8 -*-
# Joseph
import os
import re
import urllib
def getFileName(rootpath,path):
''' 获取指定目录下的所有指定后缀的文件名 '''
f_list = os.listdir(path)
for i in f_list:
# os.path.splitext():分离文件名与扩展名
if os.path.splitext(i)[1] == '.php' or os.path.splitext(i)[1] == '.txt' or os.path.splitext(i)[1] == '.xls' or os.path.splitext(i)[1] == '.rar':
Absolutepath=urllib.quote(path+i)
scandir=re.sub(rootpath,"",Absolutepath)
print urllib.unquote("/"+scandir)
def processDirectory(rootpath,dirname):
Filelist=os.listdir(dirname)
getFileName(rootpath,dirname)
for files in Filelist:
if os.path.isdir(dirname+files):
processDirectory(rootpath,dirname+files+"/")
pass
pass
def main(path):
rootpath=urllib.quote(path)
processDirectory(rootpath,path)
pass
if __name__ == '__main__':
scandir=raw_input('Please input startdir:')
main(scandir+"/") |