如何将os.walk遍历Python目录改写成长尾?

2026-04-02 00:421阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计502个文字,预计阅读时间需要3分钟。

如何将os.walk遍历Python目录改写成长尾?

访问:https://www.kancloud.cn/digest/learnpython/217656,包含目录遍历包os.path,函数os.listdir(dir)列出dirname下的目录和文件,os.getcwd()获取当前工作目录。

www.kancloud.cn/digest/learnpython/217656

目录遍历

os  os.path

函数

os.listdir(dirname)列出dirname下的目录和文件

os.getcwd()获得当前工作目录

os.curdir:返回当前目录(.)

os.chdir(dirname):改变工作目录到dirname

os.path.isdir(name):判断name是不是一个目录name不是目录就返回false

os.path.isfile(name):判断name是不是一个文件不存在name也返回false

os.path.exists(name):判断是否存在文件或目录name

os.path.getsize(name):获得文件大小如果name是目录返回0

os.path.abspath(name):获得绝对路径

os.path.normpath(path):规范path字符串形式

os.path.split(name):分割文件名与目录(事实上如果你完全使用目录它也会将最后一个目录作为文件名而分离同时它不会判断文件或目录是否存在)

os.path.splitext():分离文件名与扩展名

os.path.join(path,name):连接目录与文件名或目录

os.path.basename(path):返回文件名

os.path.dirname(path):返回文件路径

递归法

#coding:utf8

import os

def dirList(path,allfile):

filelistos.listdir(path)

for filename in filelist:

filepathos.path.join(path,filename)

if os.path.isdir(filepath):

dirList(filepath,allfile)

allfile.append(filepath)

allfile[]

dirList(D:\\pythonPro\\test,allfile)

print allfile

如何将os.walk遍历Python目录改写成长尾?

os.walk()法

os.walk(path)返回一个生成器可以用next()方法或者for循环访问该生成器的每一个元素。他的每个部分都是一个三元组,(目录x[目录x下的目录list][目录x下面的文件list])

pathD:\\pythonPro\\test

allfile1[]

for root,dirs,files in os.walk(path):

for filename in files:

allfile1.append(os.path.join(root,filename))

for dirname in dirs:

allfile1.append(os.path.join(root,dirname))

print allfile1

os.walk()方法操作更加方便实用。如果你想要顶层目录只需要在第一次迭代后break一下即可.

本文共计502个文字,预计阅读时间需要3分钟。

如何将os.walk遍历Python目录改写成长尾?

访问:https://www.kancloud.cn/digest/learnpython/217656,包含目录遍历包os.path,函数os.listdir(dir)列出dirname下的目录和文件,os.getcwd()获取当前工作目录。

www.kancloud.cn/digest/learnpython/217656

目录遍历

os  os.path

函数

os.listdir(dirname)列出dirname下的目录和文件

os.getcwd()获得当前工作目录

os.curdir:返回当前目录(.)

os.chdir(dirname):改变工作目录到dirname

os.path.isdir(name):判断name是不是一个目录name不是目录就返回false

os.path.isfile(name):判断name是不是一个文件不存在name也返回false

os.path.exists(name):判断是否存在文件或目录name

os.path.getsize(name):获得文件大小如果name是目录返回0

os.path.abspath(name):获得绝对路径

os.path.normpath(path):规范path字符串形式

os.path.split(name):分割文件名与目录(事实上如果你完全使用目录它也会将最后一个目录作为文件名而分离同时它不会判断文件或目录是否存在)

os.path.splitext():分离文件名与扩展名

os.path.join(path,name):连接目录与文件名或目录

os.path.basename(path):返回文件名

os.path.dirname(path):返回文件路径

递归法

#coding:utf8

import os

def dirList(path,allfile):

filelistos.listdir(path)

for filename in filelist:

filepathos.path.join(path,filename)

if os.path.isdir(filepath):

dirList(filepath,allfile)

allfile.append(filepath)

allfile[]

dirList(D:\\pythonPro\\test,allfile)

print allfile

如何将os.walk遍历Python目录改写成长尾?

os.walk()法

os.walk(path)返回一个生成器可以用next()方法或者for循环访问该生成器的每一个元素。他的每个部分都是一个三元组,(目录x[目录x下的目录list][目录x下面的文件list])

pathD:\\pythonPro\\test

allfile1[]

for root,dirs,files in os.walk(path):

for filename in files:

allfile1.append(os.path.join(root,filename))

for dirname in dirs:

allfile1.append(os.path.join(root,dirname))

print allfile1

os.walk()方法操作更加方便实用。如果你想要顶层目录只需要在第一次迭代后break一下即可.