国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

基于Python os模塊常用命令介紹

2020-01-04 16:27:39
字體:
供稿:網(wǎng)友

1、os.name---判斷現(xiàn)在正在實(shí)用的平臺(tái),Windows返回'nt';linux返回'posix'

2、os.getcwd()---得到當(dāng)前工作的目錄。

3、os.listdir()---

python,os模塊

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 osos_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/')TrueTrue## 判斷指定對(duì)象是否存在(兩個(gè)對(duì)象均已在上述步驟中被刪除)print os.path.exists('./test_file0.txt')print os.path.exists('./test_docu0/')FalseFalse## 返回路徑的目錄和文件名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è)參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁阳县| 潜山县| 安溪县| 察哈| 阿拉善左旗| 蓬溪县| 大冶市| 繁峙县| 拉孜县| 邹平县| 永州市| 盐亭县| 东安县| 达拉特旗| 湘乡市| 磐石市| 河西区| 黔西| 常山县| 临沧市| 万全县| 昌乐县| 五华县| 浪卡子县| 吴桥县| 西吉县| 铜川市| 临高县| 广安市| 平邑县| 玉环县| 华亭县| 南雄市| 迁西县| 遂川县| 红原县| 鹤峰县| 资源县| 蕲春县| 嘉黎县| 南宫市|