Skip to content

Instantly share code, notes, and snippets.

@MOOOWOOO
Last active March 12, 2016 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MOOOWOOO/ad7e588e7f20b15e4ba9 to your computer and use it in GitHub Desktop.
Save MOOOWOOO/ad7e588e7f20b15e4ba9 to your computer and use it in GitHub Desktop.
遍历路径下的所有文件名
from os import listdir
from os.path import splitext, basename, isfile, isdir, join
def GetFileList(dir, fileList):
newDir = dir
if isfile(dir):
fileList.append(dir.decode('utf-8'))
elif isdir(dir):
for s in listdir(dir):
# 如果需要忽略某些文件夹,使用以下代码
# if s == "xxx":
# continue
newDir = join(dir, s)
GetFileList(newDir, fileList)
return fileList
list = GetFileList('/home/test/', [])
for e in list:
print e
from os import listdir
from os.path import splitext, basename, isfile, isdir, join
def GetFileList(dir, fileList):
newDir = dir
if isfile(dir):
fileList.append(dir.decode('utf-8'))
elif isdir(dir):
for s in listdir(dir):
# 如果需要忽略某些文件夹,使用以下代码
# if s == "xxx":
# continue
newDir = join(dir, s)
GetFileList(newDir, fileList)
return fileList
lists = GetFileList('/home/test/', [])
fn = sorted(lists, key=lambda x: int(splitext(basename(x))[0]))[-1]
for e in fn:
print(e)
print fn
print splitext(basename(fn))[0]
print type(fn)
/home/test/21.log
/home/test/1.log
/home/test/29.log
/home/test/16.log
/home/test/15.log
/home/test/2.log
/home/test/19.log
/home/test/18.log
/home/test/11.log
/home/test/13.log
/home/test/20.log
/home/test/22.log
/home/test/24.log
/home/test/12.log
/home/test/14.log
/home/test/10.log
/home/test/17.log
/home/test/1.log
/home/test/2.log
/home/test/10.log
/home/test/11.log
/home/test/12.log
/home/test/13.log
/home/test/14.log
/home/test/15.log
/home/test/16.log
/home/test/17.log
/home/test/18.log
/home/test/19.log
/home/test/20.log
/home/test/21.log
/home/test/22.log
/home/test/24.log
/home/jux/test/29.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment