python遍历文件夹和文件
2013-07-10 10:25
357 查看
转自:http://hi.baidu.com/wubotao/item/e7b68f952f326dbdcc80e54d
在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是目录返回0L
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):返回文件路径
#!/usr/bin/envpython
#-*- encoding:UTF-8 -*-
importos,time,stat
fileStats =os.stat ( 'test.txt') #获取文件/目录的状态
fileInfo = {
'Size':fileStats [ stat.ST_SIZE], #获取文件大小
'LastModified':time.ctime( fileStats [ stat.ST_MTIME ]),
#获取文件最后修改时间
'LastAccessed':time.ctime(fileStats [ stat.ST_ATIME ] ),
#获取文件最后访问时间
'CreationTime':time.ctime( fileStats [ stat.ST_CTIME ]),
#获取文件创建时间
'Mode':fileStats [ stat.ST_MODE] #获取文件的模式
}
#print fileInfo
for field infileInfo: #显示对象内容
print '%s:%s' % (field,fileInfo[field])
forinfoField,infoValue in fileInfo:
print '%s:%s' % (infoField,infoValue)
if stat.S_ISDIR ( fileStats [ stat.ST_MODE ]): #判断是否路径
print 'Directory. '
else:
print 'Non-directory.'
if stat.S_ISREG(fileStats [ stat.ST_MODE ]): #判断是否一般文件
print 'Regular file.'
elif stat.S_ISLNK ( fileStats [ stat.ST_MODE ]): #判断是否链接文件
print 'Shortcut.'
elif stat.S_ISSOCK ( fileStats [ stat.ST_MODE ]): #判断是否套接字文件
print 'Socket.'
elif stat.S_ISFIFO ( fileStats [ stat.ST_MODE ]): #判断是否命名管道
print 'Named pipe.'
elif stat.S_ISBLK ( fileStats [ stat.ST_MODE ]): #判断是否块设备
print 'Block special device.'
elif stat.S_ISCHR ( fileStats [ stat.ST_MODE ]): #判断是否字符设置
print 'Character special device.'
os.remove(dir) #dir为要删除的文件夹或者文件路径
os.rmdir(path) #path要删除的目录的路径。需要说明的是,使用os.rmdir删除的目录必须为空目录,否则函数出错。
删除目录下的svn代码:
Code
#!/usr/bin/env python
#coding=utf-8
import sys, os, stat
def walk(path):
for item in os.listdir(path):
subpath = os.path.join(path, item)
mode = os.stat(subpath)[stat.ST_MODE]
if stat.S_ISDIR(mode):
if item == ".svn":
print "Cleaning %s " %subpath
print "%d deleted" % purge(subpath)
else:
walk(subpath)
def purge(path):
count = 0
for item in os.listdir(path):
subpath = os.path.join(path, item)
mode = os.stat(subpath)[stat.ST_MODE]
if stat.S_ISDIR(mode):
count += purge(subpath)
else:
os.chmod(subpath, stat.S_IREAD|stat.S_IWRITE)
os.unlink(subpath)
count += 1
os.rmdir(path)
count += 1
return count
if len(sys.argv) != 2:
print "Usage: python cleansvn.py path"
sys.exit(1)
walk(sys.argv[1])
删除某目录下所有文件和文件夹:
Code
#!/usr/bin/env python
#coding=utf-8
import os
def delete_all_file(path):
"delete all folers and files"
if os.path.isfile(path):
try:
os.remove(path)
except:
pass
elif os.path.isdir(path):
for item in os.listdir(path):
itemsrc = os.path.join(path, item)
delete_all_file(itemsrc)
try:
os.rmdir(path)
except:
pass
if __name__ == "__main__":
dirname = r'F:\trunk'
print delete_all_file(dirname)
相关文章推荐
- python 遍历文件夹 文件
- python递归遍历文件夹检查文件
- Python 遍历文件夹中的指定类型文件
- 【Python】遍历文件夹中所有文件
- python 遍历文件夹 文件
- Python遍历文件夹和读写文件的方法
- python 遍历文件夹 文件
- Python 文件夹遍历和文件查找
- python练习(遍历所有文件夹及文件)
- Python脚本- 递归遍历文件夹,获取指定扩展名文件,修改文件内容
- Python遍历文件夹,删除空文件夹,过滤特定大小和格式的文件
- python遍历文件夹——两种遍历本地文件记录文件夹个数、文件数及文件大小的方法
- python下遍历文件夹中所有文件
- 【Python】遍历文件夹,并命名文件
- Python遍历文件夹 处理json文件
- python 遍历指定文件夹下的所有文件内容
- windows下用c++和python遍历目录下的文件夹并删除所有文件名为xx的文件
- Python:遍历输出某路径下的所有文件和文件夹
- 如何用python遍历文件夹下的所有excel文件
- python遍历文件夹找出文件夹后缀为py的文件