python--文件操作之遍历目录
2016-05-21 19:39
585 查看
Python的os模块,包含了普遍的操作系统功能,这里主要学习与路径相关的函数:
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):返回文件路径
1、os.path方法
通过传入需要遍历的目录,列出目录下的所有文件并统计文件数,os提供的path模块能对目录非常灵活的操作。
import os,sys
def listdir(dir,file):
file.write(dir + '\n')
fielnum = 0
list = os.listdir(dir) #列出目录下的所有文件和目录
for line in list:
filepath = os.path.join(dir,line)
if os.path.isdir(filepath): #如果filepath是目录,则再列出该目录下的所有文件
myfile.write(' ' + line + '\\'+'\n')
for li in os.listdir(filepath):
myfile.write(' '+li + '\n')
fielnum = fielnum + 1
elif os.path: #如果filepath是文件,直接列出文件名
myfile.write(' '+line + '\n')
fielnum = fielnum + 1
myfile.write('all the file num is '+ str(fielnum))
dir = raw_input('please input the path:')
myfile = open('list.txt','w')
2、os.walk方法
os模块提供的walk方法很强大,能够把给定的目录下的所有目录和文件遍历出来。
方法:os.walk(path),遍历path,返回一个对象,他的每个部分都是一个三元组,('目录x',[目录x下的目录list],目录x下面的文件)
import os
def walk_dir(dir,fileinfo,topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
print(os.path.join(name))
fileinfo.write(os.path.join(root,name) + '\n')
for name in dirs:
print(os.path.join(name))
fileinfo.write(' ' + os.path.join(root,name) + '\n')
dir = raw_input('please input the path:')
fileinfo = open('list.txt','w')
walk_dir(dir,fileinfo)
topdown决定遍历的顺序,如果topdown为True,则先列举top下的目录,然后是目录的目录,依次类推,反之,则先递归列举出最深层的子目录,然后是其兄弟目录,然后子目录。
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):返回文件路径
1、os.path方法
通过传入需要遍历的目录,列出目录下的所有文件并统计文件数,os提供的path模块能对目录非常灵活的操作。
import os,sys
def listdir(dir,file):
file.write(dir + '\n')
fielnum = 0
list = os.listdir(dir) #列出目录下的所有文件和目录
for line in list:
filepath = os.path.join(dir,line)
if os.path.isdir(filepath): #如果filepath是目录,则再列出该目录下的所有文件
myfile.write(' ' + line + '\\'+'\n')
for li in os.listdir(filepath):
myfile.write(' '+li + '\n')
fielnum = fielnum + 1
elif os.path: #如果filepath是文件,直接列出文件名
myfile.write(' '+line + '\n')
fielnum = fielnum + 1
myfile.write('all the file num is '+ str(fielnum))
dir = raw_input('please input the path:')
myfile = open('list.txt','w')
2、os.walk方法
os模块提供的walk方法很强大,能够把给定的目录下的所有目录和文件遍历出来。
方法:os.walk(path),遍历path,返回一个对象,他的每个部分都是一个三元组,('目录x',[目录x下的目录list],目录x下面的文件)
import os
def walk_dir(dir,fileinfo,topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
print(os.path.join(name))
fileinfo.write(os.path.join(root,name) + '\n')
for name in dirs:
print(os.path.join(name))
fileinfo.write(' ' + os.path.join(root,name) + '\n')
dir = raw_input('please input the path:')
fileinfo = open('list.txt','w')
walk_dir(dir,fileinfo)
topdown决定遍历的顺序,如果topdown为True,则先列举top下的目录,然后是目录的目录,依次类推,反之,则先递归列举出最深层的子目录,然后是其兄弟目录,然后子目录。
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法