python基础------os模块

os模块

os模块的作用:

  os,语义为操作系统,所以肯定就是操作系统相关的功能了,可以处理文件和目录这些我们日常手动需要做的操作,就比如说:显示当前目录下所有文件/删除某个文件/获取文件大小……

os模块的常用功能:

1  os.name      #显示当前使用的平台

1 >>> os.name
2 'nt'                  #这表示Windows
3 >>> os.name
4 'posix'             #这表示Linux
View Code

2  os.getcwd()      #显示当前python脚本工作路径

1 >>> os.getcwd()
2 'C:\Users\Capital-D\PycharmProjects\untitled'    #使用pycharm
3 
4 >>> os.getcwd()
5 '/root'         #Linux平台在/root目录直接使用python3命令
View Code

3  os.listdir('dirname')        #返回指定目录下的所有文件和目录名

1 #相对于os.getcwd路径下的文件
2 >>> os.listdir()
3 ['.idea', 'test']
4 
5 
6 >>> os.listdir()
7 ['.bash_logout', 'Python-3.4.4', '.mysql_history', '.tcshrc', 'Python-3.4.4.tar.xz', '.bash_profile', '.lesshst', 'install.log.syslog', '.cshrc', '04.sql', 'anaconda-ks.cfg', 'test', '.viminfo', 'phpMyAdmin-4.4.15-all-languages.tar.bz2', '1test', '.bashrc', 'binlog.sql', 'back.sql', 'install.log', 'binlog4.sql', '.bash_history', 'backup.sql', 'text.py', '.rnd', 'test1']
View Code

4  os.remove('filename')       #删除一个文件

 1 [root@slyoyo ~]# touch hahaha
 2 [root@slyoyo ~]# ls
 3 04.sql           back.sql     binlog.sql   install.log.syslog                       Python-3.4.4.tar.xz  text.py
 4 1test            backup.sql   hahaha       phpMyAdmin-4.4.15-all-languages.tar.bz2  test
 5 anaconda-ks.cfg  binlog4.sql  install.log  Python-3.4.4                             test1
 6 #hahaha(粉色字体)存在
 7 [root@slyoyo ~]# python3
 8 Python 3.4.4 (default, Apr  5 2016, 04:23:19) 
 9 [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux
10 Type "help", "copyright", "credits" or "license" for more information.
11 >>> import os
12 >>> os.remove('hahaha')
13 >>> exit()
14 [root@slyoyo ~]# ls
15 04.sql  anaconda-ks.cfg  backup.sql   binlog.sql   install.log.syslog                       Python-3.4.4         test   text.py
16 1test   back.sql         binlog4.sql  install.log  phpMyAdmin-4.4.15-all-languages.tar.bz2  Python-3.4.4.tar.xz  test1
17 #hahaha已被删
View Code

5  os.makedirs('dirname/dirname')     #可生成多层递规目录

 1 [root@slyoyo ~]# ls
 2 04.sql  anaconda-ks.cfg  backup.sql   binlog.sql   install.log.syslog                       Python-3.4.4         test   text.py
 3 1test   back.sql         binlog4.sql  install.log  phpMyAdmin-4.4.15-all-languages.tar.bz2  Python-3.4.4.tar.xz  test1
 4 [root@slyoyo ~]# python3
 5 Python 3.4.4 (default, Apr  5 2016, 04:23:19) 
 6 [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux
 7 Type "help", "copyright", "credits" or "license" for more information.
 8 >>> import os
 9 >>> os.makedirs('hahaha/linghuchong')
10 >>> exit()
11 [root@slyoyo ~]# ls
12 04.sql           back.sql     binlog.sql   install.log.syslog                       Python-3.4.4.tar.xz  text.py
13 1test            backup.sql   hahaha       phpMyAdmin-4.4.15-all-languages.tar.bz2  test
14 anaconda-ks.cfg  binlog4.sql  install.log  Python-3.4.4                             test1
15 [root@slyoyo ~]# ls hahaha/
16 linghuchong
17 [root@slyoyo ~]# ls hahaha/linghuchong/
18 [root@slyoyo ~]#
View Code

6  os.rmdir('dirname')     #删除单级目录

 1 [root@slyoyo ~]# ls
 2 04.sql           back.sql     binlog.sql   install.log.syslog                       Python-3.4.4.tar.xz  text.py
 3 1test            backup.sql   hahaha       phpMyAdmin-4.4.15-all-languages.tar.bz2  test
 4 anaconda-ks.cfg  binlog4.sql  install.log  Python-3.4.4                             test1
 5 [root@slyoyo ~]# ls hahaha/
 6 linghuchong
 7 [root@slyoyo ~]# ls hahaha/linghuchong/
 8 [root@slyoyo ~]# python3
 9 Python 3.4.4 (default, Apr  5 2016, 04:23:19) 
10 [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux
11 Type "help", "copyright", "credits" or "license" for more information.
12 >>> import os
13 >>> os.rmdir('hahaha/linghuchong')
14 >>> exit()
15 [root@slyoyo ~]# ls hahaha/
16 [root@slyoyo ~]#
View Code

7  os.rename("oldname","newname")    #重命名文件

1 >>> os.getcwd()
2 '/root/hahaha'
3 >>> os.listdir()
4 ['test']
5 >>> os.rename('test','test_new')
6 >>> os.listdir()
7 ['test_new']
View Code

8  os.system()    #运行shell命令,注意:这里是打开一个新的shell,运行命令,当命令结束后,关闭shell

1 >>> os.system('pwd')
2 /root/hahaha
3 0
View Code

9  os.sep    #显示当前平台下路径分隔符

1 >>> os.sep
2 '/'               #linux
3 
4 >>> os.sep
5 '\'             #windows
View Code

10  os.linesep    #给出当前平台使用的行终止符

1 >>> os.linesep
2 '
'      #linux
3 
4 >>> os.linesep
5 '
'    #windows
View Code

11  os.environ    #获取系统环境变量

 View Code

12  os.path.abspath(path)    #显示当前绝对路径

 View Code

13  os.path.dirname(path)    #返回该路径的父目录

 View Code

14  os.path.basename(path)    #返回该路径的最后一个目录或者文件,如果path以/或结尾,那么就会返回空值。

 View Code

15  os.path.isfile(path)     #如果path是一个文件,则返回True

 View Code

16  os.path.isdir(path)    #如果path是一个目录,则返回True

 View Code

17  os.stat()    #获取文件或者目录信息

 View Code

18  os.path.split(path)  #将path分割成路径名和文件名。(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)

 View Code

19  os.path.join(path,name)   #连接目录与文件名或目录 结果为path/name

 View Code
原文地址:https://www.cnblogs.com/lhy-522/p/13020312.html