Chinaunix首页 | 论坛 | 博客
  • 博客访问: 166974
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 425
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-13 17:05
文章分类

全部博文(31)

文章存档

2016年(11)

2015年(20)

我的朋友

分类: LINUX

2015-12-30 16:05:39

一、Python os模块包含普遍的操作系统功能。如果希望程序和平台无关的话,这个模块尤为重要。

二、常用方法:
    
    1、os.name:输出字符串指示的平台。如果是win则是‘nt’表示,对于linux则是'posix'
  1. In [1]: import os

  2. In [2]: os.name
  3. Out[2]: 'posix'
    2、os.getcwd():函数得到当前工作目录。
  1. In [3]: os.getcwd()
  2. Out[3]: '/root'
    3、os.chdir(): 改变当前工作目录
  1. In [5]: os.chdir('/tmp')
  2. In [6]: os.getcwd()
  3. Out[6]: '/tmp'
    4、os.listdir('/tmp'):列出指定目录下的所有文件名
  1. In [7]: os.listdir('/tmp')
  2. Out[7]: ['test1.txt', 'test2.txt', 'shadow', 'passwd', '.ICE-unix', 'bcd']
    5、os.remove():删除一个文件
  1. In [11]: os.remove('/tmp/shadow')

  2. In [12]: os.listdir('/tmp')
  3. Out[12]: ['test1.txt', 'test2.txt', 'passwd', '.ICE-unix', 'bcd']
    6、os.system():运行shell命令

  1. In [17]: os.system('ps')
  2.    PID TTY TIME CMD
  3.   1719 pts/0 00:00:00 bash
  4.   2772 pts/0 00:00:00 ipython
  5.   2800 pts/0 00:00:00 ps
  6. Out[17]: 0
    7、os.path.split():返回参数的目录名和文件名

  1. In [20]: os.path.split('/etc/profile.d/mysql.sh')
  2. Out[20]: ('/etc/profile.d', 'mysql.sh')
    8、os.path.isfile() 和 os.path.isdir() 判断给出的路径是文件还是目录

  1. In [21]: os.path.isfile('/etc/passwd')
  2. Out[21]: True

  3. In [22]: os.path.isdir('/etc/passwd')
  4. Out[22]: False
9、os.path.exists():判断给出的路径是真实存在还是假的

  1. In [25]: os.path.exists('/etc/')
  2. Out[25]: True

  3. In [26]: os.path.exists('/etc/abaa')
  4. Out[26]: False
10、os.path.abspath():获得绝对路径

  1. In [29]: os.path.abspath('../etc/')
  2. Out[29]: '/etc'
11、os.path.getsize(): 获取文件大小。单位:B

  1. In [34]: os.path.getsize('/etc/passwd')
  2. Out[34]: 1338
12、os.path.splitext():分离文件名和扩展名

  1. In [35]: os.path.splitext('/etc/my.cnf')
  2. Out[35]: ('/etc/my', '.cnf')
13、os.path.join():连接文件名和文件或目录

  1. In [36]: os.path.join('/etc','my.cnf')
  2. Out[36]: '/etc/my.cnf'

  3. In [38]: os.path.join('/etc','profile.d')
  4. Out[38]: '/etc/profile.d'
14、os.path.basename():返回文件名

  1. In [40]: os.path.basename('/etc/my.cnf')
  2. Out[40]: 'my.cnf'
15、os.path.dirname():返回目录

  1. In [41]: os.path.dirname('/etc/profile.d/mysql.sh')
  2. Out[41]: '/etc/profile.d'
16、os.mkdir(): 创建指定目录
  1. In [49]: os.mkdir('test')

  2. In [50]: os.listdir('.')
  3. Out[50]: ['test']
17、os.makedirs(): 递归创建目录
  1. In [51]: os.makedirs('a/b/c')

  2. In [52]: os.listdir('.')
  3. Out[52]: ['a', 'test']
18、os.rmdir(): 删除目录
  1. In [55]: os.rmdir('a/b/c')
  2. In [56]: os.listdir('./a/b')
  3. Out[56]: []
19、os.removedirs():  删除多级目录
  1. In [59]: os.removedirs('a/b')

  2. In [60]: os.listdir('/tmp')
  3. Out[60]: ['test']
20、os.remove(): 删除文件
  1. In [66]: os.remove('abc')

  2. In [67]: os.listdir('.')
  3. Out[67]: ['test']
21、os.unlink(): 删除链接文件
  1. In [70]: os.unlink('/tmp/file1')

  2. In [71]: os.listdir('/tmp')
  3. Out[71]: ['abc', 'test']
22、os.rename(): 文件重命名
  1. In [72]: os.listdir('/tmp')
  2. Out[72]: ['abc', 'test']

  3. In [73]: os.rename('abc','file1')

  4. In [74]: os.listdir('/tmp')
  5. Out[74]: ['file1', 'test']
23、os.stat():    返回文件状态信息

点击(此处)折叠或打开

  1. In [75]: os.stat('/tmp/file1')
  2. Out[75]: posix.stat_result(st_mode=33188, st_ino=392780, st_dev=64768L, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1450927901, st_mtime=1450927901, st_ctime=1450928053)
24、os.symlink(): 创建符号链接,源需绝对路径
  1. In [78]: os.symlink('/tmp/file1','/tmp/l_file2')
  2. In [79]: os.listdir('/tmp')
  3. Out[79]: ['file1', 'test', 'l_file2']
阅读(1547) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~