`

python 文件系统

 
阅读更多
文件处理
mkfifo()/mknod()
a
创建命名管道/创建文件系统节点
remove()/unlink()  Delete file 删除文件
rename()/renames()
b
重命名文件
*stat
c
() 返回文件信息
symlink() 创建符号链接
utime()  更新时间戳
tmpfile() 创建并打开('w+b')一个新的临时文件
walk()
a
生成一个目录树下的所有文件名  目录/文件夹
chdir()/fchdir()
a
改变当前工作目录/通过一个文件描述符改变当前工作目录
chroot()
d
改变当前进程的根目录
listdir()  列出指定目录的文件
getcwd()/getcwdu()
a
返回当前工作目录/功能相同, 但返回一个 Unicode 对象
mkdir()/makedirs() 创建目录/创建多层目录
rmdir()/removedirs()  删除目录/删除多层目录
访问/权限
access() 检验权限模式
chmod()   改变权限模式
chown()/lchown()
a
改变 owner 和 group ID/功能相同, 但不会跟踪链接
umask()  设置默认权限模式
文件描述符操作
open()  底层的操作系统 open (对于文件, 使用标准的内建 open() 函数)
read()/write()  根据文件描述符读取/写入数据
dup()/dup2()  复制文件描述符号/功能相同, 但是是复制到另一个文件描述符
设备号
makedev()
a
从 major 和 minor 设备号创建一个原始设备号
Edit By Vheavens
Edit By Vheavens
major()
a
/minor()
a
从原始设备号获得 major/minor 设备号


os.path 模块中的路径名访问函数
basename()  去掉目录路径, 返回文件名
dirname()  去掉文件名, 返回目录路径
join()   将分离的各部分组合成一个路径名
split()   返回 (dirname(), basename()) 元组
splitdrive()  返回 (drivename, pathname) 元组
splitext()  返回 (filename, extension) 元组
信息
getatime()   返回最近访问时间
getctime()   返回文件创建时间
getmtime()   返回最近文件修改时间
getsize()   返回文件大小(以字节为单位)

查询
exists()   指定路径(文件或目录)是否存在
isabs()   指定路径是否为绝对路径
isdir()   指定路径是否存在且为一个目录
isfile()  指定路径是否存在且为一个文件
islink()  指定路径是否存在且为一个符号链接
ismount()   指定路径是否存在且为一个挂载点
samefile()   两个路径名是否指向同个文件




# -*- coding: cp936 -*-
import os
tmpdir='C:\\temp'
if os.path.isdir(tmpdir):
print 'is dir'
else:
print 'not temp directory available'
#tmpdir=''
if tmpdir:
os.chdir(tmpdir)
#跳转到tmpdir目录下
cwd=os.getcwd()
#获取当前路径
print '***current temporary directory'
print cwd
print '***creating example directory...'
os.mkdir('example')
#创建文件夹
os.chdir('example')
cwd=os.getcwd()
print '**** new  working directroy'
print cwd
print '*** original directory listing'
print os.listdir(cwd)#列出cwd下所有目录和文件
print '*****creating test file...'
fobj=open('test','w')
fobj.write('foo\n')
fobj.write('bar\n')
fobj.close()
print '***updated directory listing'
print os.listdir(cwd)
#列出cwd下所有目录和文件
print """***renaming 'test' to 'filetest'"""
os.rename('test','filetest.txt')
print '****updated directory listing:'
print os.listdir(cwd)
path=os.path.join(cwd,os.listdir(cwd)[0])
print '*** full file pathname'
print path
print '**(pathname,basename)=='
print os.path.split(path)
#分割文件名和路径(事实上,如果你完全使用目录也会将uohou一个目录昨晚文件名而分离,
#同时他不会判断文件或目录是否存在
print '***(filename,extension)=='
print os.path.splitext(os.path.basename(path))
#os.path.splitext()分离文件名与扩展名
#os.path.basename() 返回文件名
print '*** displaying file contents:'
fobj=open(path)
#path=c:\\temp\\example\\filetest.txt
'''
循环filetest.txt
'''
for eachLine in fobj:
print eachLine,
fobj.close()
print '***deleting test file'
print 'path',path
os.remove(path)
#删除filetest。txt文件
print '***updated directory listing:'
print cwd
print os.listdir(cwd)
print 'os.pardir',os.pardir
os.chdir(os.pardir)
#返回上级目录temp
print '***deleting test directory'
os.rmdir('example')
#删除example文件夹
print '***DONE'


运行结果:
>>> ================================ RESTART ================================
>>>
is dir
***current temporary directory
C:\temp
***creating example directory...
**** new  working directroy
C:\temp\example
*** original directory listing
[]
*****creating test file...
***updated directory listing
['test']
***renaming 'test' to 'filetest'
****updated directory listing:
['filetest.txt']
*** full file pathname
C:\temp\example\filetest.txt
**(pathname,basename)==
('C:\\temp\\example', 'filetest.txt')
***(filename,extension)==
('filetest', '.txt')
*** displaying file contents:
foo
bar
***deleting test file
path C:\temp\example\filetest.txt
***updated directory listing:
C:\temp\example
[]
os.pardir ..
***deleting test directory
***DONE
>>>


===========================================================================
统计文件中行数
import os
tmp='c:\\python27\\1.py'
lines=0
objf=open(tmp)
for ff in objf:
print ff
lines+= ff.count('\n')
print 'lines=%d'%lines
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics