os模块

import os
from conf import setting

BASE_PATH = os.path.dirname(
os.path.dirname(os.path.abspath(__file__))
)

print(__file__) #linux、mac 下,当前文件所在的路径/Users/liuyan/PycharmProjects/ly-code/day10/练习.py
#windows下是 c:/Users/egova/**/练习.py
print(os.path.abspath(__file__)) # linux下和__file__一样;windows下 c:\Users\egova\**\练习.py

print(os.path.dirname(os.path.abspath(__file__))) # 当前文件的路径 /Users/liuyan/PycharmProjects/ly-code/day10

print(os.listdir(setting.CASE_PATH)) # list 形式返回路径下的文件 ['测试用例.xls']

print(os.walk(os.path.dirname(os.path.abspath(__file__))))
#返回的数据类型为 <class 'generator'> yield top, dirs, nondirs
#目录有多层,把所有层级目录下的文件都列出来,列表里的元素为 元组,元素分别是:路径、文件夹名、该文件夹下的文件
#用法例子:for abs_path,dir,file in os.walk(path):
# print(abs_path,dir,file)

os.system('ipconfig') #执行操作系统命令,但不返回结果
print(os.popen('ipconfig').read()) #执行操作系统命令,且返回执行结果
print(os.getcwd()) #取当前工作目录

print(os.path.join('dir','**.py')) #拼接成一个路径,兼容windows ,linux 的路径

posted @ 2018-05-30 15:26  liuyanerfly  阅读(126)  评论(0编辑  收藏  举报