Python中的os模块


本文摘自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 讲解进制转换 int、bin、oct、hex

Python怎么随机产生一个范围内的数

Python中两个斜杠是什么运算

Python中pandas和xlsxwriter读写xlsx文件的方法介绍(附代码)

Python的cmd命令行在哪里

Python中常用到的#是什么意思

Python开发什么

Python如何将数据导出excel的技巧分享

Python实现关键词提取的示例讲解

Python关于变量赋值的秘密介绍

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...