python os模块 经常使用命令

# -*- coding:utf-8 -*-
#os模块
import os
#输出字符串指示正在使用的平台。若是是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'。
print os.name
#获取当前目录
print os.getcwd()
#返回指定目录下的全部文件和目录名。
print os.listdir(os.getcwd())
#删除文件
os.remove('/opt/case/a.txt')
#运行shell命令。
os.system('cmd')
#返回一个路径的目录名和文件名
print os.path.split('/opt/case/a.txt')
#函数用来检验给出的路径是否真地存在
print os.path.exists(os.getcwd())
#得到绝对路径
print os.path.abspath('../')
#得到文件大小,若是name是目录返回0L
print os.path.getsize('D:/Python27/README.txt')
#分离文件名与扩展名=> ('README', '.txt')
print os.path.splitext('README.txt')
#返回文件名=>README.txt
print os.path.basename('/opt/case/README.txt')
#:返回文件路径=>/opt/case
print os.path.dirname('/opt/case/README.txt')
#建立目录
os.mkdir('./a')
#删除目录
os.rmdir('./a')
相关文章
相关标签/搜索