本文摘自php中文网,作者巴扎黑,侵删。
Python文件系统功能:os模块1.os模块方法分类
(1)目录:
1 2 3 4 5 6 7 8 | chdir () 改变工作目录
chroot () 设定当前进程的根目录
listdir() 列出指定目录下的所有文件名
mkdir () 创建指定目录
makedirs() 创建多级目录
getcwd () 返回当前工作目录
rmdir () 删除指定目录
removedirs() 删除多级目录
|
(2)文件:
1 2 3 4 5 6 7 8 9 | mkinfo() 创建管道
mknod() 创建设备文件
remove() 删除文件
unlink() 删除链接文件
rename() 重命名
stat() 返回文件状态信息
symlink() 创建符号链接
utime() 更新时间戳
tmpfile() 创建并打开(w+b)一个新的临时文件
|
(3)访问权限
1 2 3 4 | access(path, mode) 判断指定用户是否有访问权限 os.access( '/tmp' ,0) uid为0用户是否有权限访问/tmp目录
chmod (path,mode) 修改权限 os. chmod ( '/tmp/s' ,0640) 将/tmp/s 权限修改为640
chown (path,uid,gid) 修改属主、属组
umask() 设置默认权限模式 os.umask(022)
|
(4)设备文件
1 2 3 | makedev() 创建设备
major() 指定设备获取主设备号
minor() 指定设备获取次设备号
|
(5)文件描述符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | open() 较低的IO打开
read() 较低的IO读
write() 较低的IO写
4、5相对用的少
补充:
os.walk() 相当于tree命令
>>> import os
>>> a1 = os.walk( '/root' )
>>> a1.next()
( '/root' ,
[ '.subversion' , '.ssh' , '.ipython' , '.pki' , '.cache' ],
[ 'test.py' ,
'.bash_history' ,
'.cshrc' ,
'.bash_logout' ,
'.tcshrc' ,
'.bash_profile' ,
'.mysql_history' ,
'.bashrc' ,
'.viminfo' ])
返回一个元组,由(文件名,[文件夹],[文件]) 组成
|
2.os模块中的path模块
1)跟文件路径相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | basename () 路径基名
dirname() 路径目录名
join() 整合文件名
split() 返回dirname(), basename ()元组
splitext() 返回(filename,extension)元组
例:
>>> dir1 = os.path.dirname( '/etc/sysconfig/iptables-config' )
>>> dir1
'/etc/sysconfig'
>>> file1 = os.path. basename ( '/etc/sysconfig/iptables-config' )
>>> file1
'iptables-config'
>>> os.path.join(dir1,file1)
'/etc/sysconfig/iptables-config'
>>> for filename in os.listdir( '/tmp' ):
print os.path.join( '/tmp' ,filename)
|
2)信息
1 2 3 4 | getatime() 返回文件最近一次访问时间
getmtime() 返回文件最近一次修改时间
getctime() 返回文件最近一次改变时间
getsize() 返回文件的大小
|
3)查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | exists() 判断指定文件是否存在 isabs() 判断指定的路径是否为绝对路径
isdir() 是否为目录
isfile() 是否为文件
islink() 是否符号链接
ismount() 是否为挂载点
sanefile(f1,f2) 两个路径是否指向了同一个文件
例:判断文件是否存在,存在则打开,让用户通过键盘反复输入多行数据,追加保存至此文件中
>>> import os
>>> import os.path
>>> if os.path.isfile( '/tmp/s' ):
f1 = open( '/tmp/s' , 'a+' )
while True:
a2 = raw_input( "Input >> " )
if a2 == 'q' or a2 == 'quit' :
break
f1.write(a2+ '\n' )
f1.close()
|
4)对象持久存储
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 把变量从内存中变成可存储或传输的过程称之为序列化
pickle、marshal、DBM接口、shelve模块
pickle 将内存对象持久存储在文件中
>>> import pickle
>>> dict1 = { 'x' :1, 'y' :2, 'z' : 'hello world' }
>>> f1 = open( '/tmp/s' , 'a+' )
>>> pickle.dump(dict1,f1) 通过流逝化将字典保存在文件中
>>> f1.close()
# file /tmp/s
/tmp/s: ASCII text
# cat /tmp/s
(dp0
S 'y'
p1
I2
sS 'x'
p2
I1
sS 'z'
p3
S 'hello world'
p4
s.
>>> f2 = open( '/tmp/s' , 'a+' )
>>> dict2 = pickle.load(f2) 重新装载
>>> dict2
{ 'x' :1, 'y' :2, 'z' : 'hello world' }
|
以上就是Python中的os模块的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python中协程实现tcp连接的代码示例
Python如何判断一个文件是否存在
Python字典dict类型合并详解
Python为什么要装32位的
Python中lambda函数的用法介绍(附示例)
Python实现蒙特卡罗方法(代码示例)
Python中关于上下文管理器的详解
Python实现网页中下载的功能实例
Python入口函数是什么
详细讲解 Python中的正则表达式
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python中的os模块