基于Python os模塊常用命令介紹
1、os.name---判斷現(xiàn)在正在實(shí)用的平臺(tái),Windows返回'nt';linux返回'posix'
2、os.getcwd()---得到當(dāng)前工作的目錄。
3、os.listdir()---

4、os.remove---刪除指定文件
5、os.rmdir()---刪除指定目錄
6、os.mkdir()---創(chuàng)建目錄(只能創(chuàng)建一層)
7、os.path.isfile()---判斷指定對(duì)象是否為文件。是則返回True。
8、os.path.isdir()---判斷指定對(duì)象是否為目錄
9、os.path.exists()---判斷指定對(duì)象是否存在。
10、os.path.split()---返回目錄的目錄和文件名。
11、os.path.join(path, name)——連接目錄和文件名。
++++++++++++++++++++++++++++++++++++++++++++
import os
os_path = '/home/meringue/Documents/PythonFile/osNotes/'
## 更改當(dāng)前工作目錄
os.chdir(os_path)
## 獲取當(dāng)前工作目錄
os.getcwd()
'/home/meringue/Documents/PythonFile/osNotes'
## 返回當(dāng)前系統(tǒng)(windows: nt; Linux: posix)
os.name
'posix'
## 創(chuàng)建文件和文件目錄
for i in range(5):
os.mknod('test_file'+str(i)+'.txt') # 文件
os.mkdir('test_docu'+str(i)) # 文件目錄
os.makedirs('./test_docu5/test_docu0/') # 多層文件夾路徑1
## 獲取指定路徑下的文件列表(不區(qū)分文件和文件夾)
os.listdir(os_path)
['.ipynb_checkpoints',
'test_docu2',
'test_docu1',
'test_docu3',
'test_file2.txt',
'test_docu4',
'test_docu5',
'osNotes.ipynb',
'test_file3.txt',
'test_docu0',
'test_file0.txt',
'test_file4.txt',
'test_file1.txt']
## 刪除當(dāng)前目錄下指定文件或文件夾
os.remove('./test_file0.txt') # 文件
os.rmdir('./test_docu0/') # 文件夾
## 判斷指定對(duì)象是否為文件或目錄(返回True或False)
print os.path.isfile('./test_file1.txt')
print os.path.isdir('./test_docu5/test_docu0/')
True
True
## 判斷指定對(duì)象是否存在(兩個(gè)對(duì)象均已在上述步驟中被刪除)
print os.path.exists('./test_file0.txt')
print os.path.exists('./test_docu0/')
False
False
## 返回路徑的目錄和文件名
print os.path.split(os_path)
print os.path.split(os_path+'test_file1.txt')
('/home/meringue/Documents/PythonFile/osNotes', '')
('/home/meringue/Documents/PythonFile/osNotes', 'test_file1.txt')
## 返回絕對(duì)路徑
print os.path.abspath('./test_file1.txt')
print os.path.abspath('./test_docu1/')
/home/meringue/Documents/PythonFile/osNotes/test_file1.txt
/home/meringue/Documents/PythonFile/osNotes/test_docu1
## 連接目錄和文件名
os.path.join(os_path,'test_file1.txt')
'/home/meringue/Documents/PythonFile/osNotes/test_file1.txt'
## 返回文件名和文件路徑
print os.path.basename(os_path+'test_file1.txt')
print os.path.dirname(os_path+'test_file1.txt')
test_file1.txt
/home/meringue/Documents/PythonFile/osNotes
以上這篇基于Python os模塊常用命令介紹就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用MySQLdb for Python操作數(shù)據(jù)庫(kù)教程
這篇文章主要介紹了Python使用MySQLdb for Python操作數(shù)據(jù)庫(kù)教程,詳細(xì)講述了MySQLdb的用法,針對(duì)Python操作MySQL數(shù)據(jù)庫(kù)程序設(shè)計(jì)具有很好的參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10
Python實(shí)現(xiàn)郵件發(fā)送的詳細(xì)設(shè)置方法(遇到問(wèn)題)
這篇文章主要介紹了Python實(shí)現(xiàn)郵件發(fā)送的詳細(xì)設(shè)置方法(遇到問(wèn)題),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Django+Ajax+jQuery實(shí)現(xiàn)網(wǎng)頁(yè)動(dòng)態(tài)更新的實(shí)例
今天小編就為大家分享一篇Django+Ajax+jQuery實(shí)現(xiàn)網(wǎng)頁(yè)動(dòng)態(tài)更新的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
keras實(shí)現(xiàn)調(diào)用自己訓(xùn)練的模型,并去掉全連接層
這篇文章主要介紹了keras實(shí)現(xiàn)調(diào)用自己訓(xùn)練的模型,并去掉全連接層,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
matplotlib圖形整合之多個(gè)子圖繪制的實(shí)例代碼
matplotlib繪制多個(gè)子圖的時(shí)候,我們可以根據(jù)自己的想法去排列子圖的順序,也可以生成不同的子圖數(shù)量,本文就詳細(xì)的介紹了matplotlib 多子圖繪制,具有一定的參考價(jià)值,感興趣的可以了解一下2022-04-04

