[TOC]
<br>
### os模塊介紹
os模塊主要用于程序與操作系統之間的交互。 在os.py源碼中,有一段說明很重要,如下:
```python
r"""OS routines for NT or Posix depending on what system we're on.
This exports:
- all functions from posix, nt or ce, e.g. unlink, stat, etc.
- os.path is either posixpath or ntpath
- os.name is either 'posix', 'nt' or 'ce'.
- os.curdir is a string representing the current directory ('.' or ':')
- os.pardir is a string representing the parent directory ('..' or '::')
- os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
- os.extsep is the extension separator (always '.')
- os.altsep is the alternate pathname separator (None or '/')
- os.pathsep is the component separator used in $PATH etc
- os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
- os.defpath is the default search path for executables
- os.devnull is the file path of the null device ('/dev/null', etc.)
Programs that import and use 'os' stand a better chance of being
portable between different platforms. Of course, they must then
only use functions that are defined by all platforms (e.g., unlink
and opendir), and leave all pathname manipulation to os.path
(e.g., split and join).
"""
```
從這段描述可知,在python我們使用相同的os模塊與不同的平臺,如windows或linux交互。
os模塊中有非常多的變量和方法,這里就不一一介紹,還是那句話,`源碼才是最好的學習資料`。
### os 模塊常用變量
```python
>>> os.name # 顯示當前系統名稱,win->'nt'; Linux->'posix'
'nt'
>>> os.sep # 顯示當前系統中,路徑分割符
'\\'
>>> os.linesep # 顯示當前系統的文本換行符
'\r\n'
>>> os.environ # 顯示系統環境變量
```
### os 模塊常用方法
```python
>>> os.getcwd() # 注意:是當前運行腳本的路徑,不是python腳本文件的路徑
'D:\\os_exec'
# 與目錄相關的方法
>>> os.mkdir('tmp') # 創建文件夾tmp,如果tmp已存在,則會報錯哦
>>> os.rmdir('tmp') # 刪除空文件夾,如果文件夾不為空或不存在,都會報錯哦
>>> os.makedirs('tmp4/tmp44') # 創建多級文件夾tmp4/tmp44
>>> os.removedirs('tmp4/tmp44') # 若目錄為空,則刪除,并遞歸到上一級目錄,如若也為空,則刪除,依此類推
>>> os.rename('src','dst')# 修改文件(夾)名稱,從src修改為dst
>>> os.listdir() # 返回目錄下的所有文件(夾)列表
['dst', 'tmp1', 'tmp2', 'tmp5', '新建文本文檔.txt']
# 與調用系統命令有關
>>> os.system('dir') # 在子shell中運行shell命令,直接顯示,返回值0表示成功,1表示失敗
# 與文件相關的方法
>>> os.remove('tmp.txt') # 刪除文件
```
**刪除非空文件夾:**
有心的朋友或許已經發現,在刪除文件夾時,如果文件夾非空,則用os是無法刪除的。此時可以使用`shutil`庫,該庫為python內置庫,是一個對文件及文件夾高級操作的庫,可以與os庫互補完成一些操作,如文件夾的整體復制,移動文件夾,對文件重命名等。
```python
shutil.rmtree(path) #遞歸刪除文件夾
```
### os.path 模塊常用方法
```python
>>> os.path.abspath("tmp1") # 返回path規范化的絕對路徑
'D:\\os_exec\\tmp1'
>>> os.path.split("D://os_exec/test.txt") # 將path分割成目錄和文件名二元組返回
('D://os_exec', 'test.txt')
>>> os.path.dirname("D://os_exec/test.txt") # 返回path的目錄,同os.path.split(path)的第一個元素
'D://os_exec'
>>> os.path.basename("D://os_exec/test.txt") # 返回path最后的文件名(如果path以/或\結尾則返回空值)同os.path.split(path)的第二個元素
'test.txt'
>>> os.path.exists("D://os_exec/test.txt") # 判斷path是否存在
True
>>> os.path.isfile("D://os_exec/test.txt") # 判斷path是否文件
True
>>> os.path.isdir("D://os_exec/test.txt") # 判斷path是否文件夾
False
>>> os.path.join("D:\\os_exec","tmp","test.txt") # 將多個路徑組合后返回
'D:\\os_exec\\tmp\\test.txt'
>>> os.path.getatime('D:\\os_exec\\test.txt') # 獲取path新增時間
1530950642.685528
```
<hr style="margin-top:100px">
:-: 
***微信掃一掃,關注“python測試開發圈”,了解更多測試教程!***
- 前言
- chapter01_開發環境
- chapter02_字符串的使用
- chapter03_列表的使用
- chapter04_字典的使用
- chapter05_數字的使用
- chapter06_元組的使用
- chapter07_集合的使用
- chapter08_輸入輸出
- chapter09_控制流程
- chapter10_實例練習_登錄1
- chapter11_python函數入門
- chapter12_python中的類
- chapter13_輕松玩轉python中的模塊管理
- chapter14_掌握學習新模塊的技巧
- chapter15_通過os模塊與操作系統交互
- chapter16_子進程相關模塊(subprocess)
- chapter17_時間相關模塊(time & datetime)
- chapter18_序列化模塊(json)
- chapter19_加密模塊(hashlib)
- chapter20_文件的讀與寫
- chapter21_階段考核2_登錄
- chapter22_小小算法挑戰(排序&二分法)
- chapter23_用多線程來搞事!
- chapter24_HTTP接口請求(requests)
- chapter25_接口測試框架(pytest)
- chapter26_階段考核3_HTTP接口測試
- chapter27_HTML解析(pyquery)
- chapter28_階段考核4_爬蟲下載網易汽車
- chapter29_python中的那些編碼坑
- chapter30_MySQL數據庫操作
- chapter31 高級特性_迭代器與生成器
- chapter32 高級特性_裝飾器
- chapter33 高級特性_列表處理