欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python的os包與os.path模塊的用法詳情

 更新時(shí)間:2022年03月01日 10:37:48   作者:Mar丶流年  
這篇文章主要介紹了Python的os包與os.path模塊,主要介紹os常用方法和os.path?常用方法展開相關(guān)資料,需要的小伙伴可以參考一下,希望對(duì)你的工作或?qū)W習(xí)有所幫助

一、os常用方法

1.獲取當(dāng)前路徑 os.getcwd()

# coding:utf-8
import os
if __name__ == '__main__':
? ? print(os.getcwd()) ?# G:\workSpace\py_d

2.獲取指定路徑下有哪些文件和目錄,os.listdir(path)返回一個(gè)list

# coding:utf-8

import os

if __name__ == '__main__':
? ? print(os.listdir(os.getcwd())) ?# ['.idea', 'main.py', 'os_module.py', 'sk']

3.創(chuàng)建目錄(一級(jí)) os.mkdir(paht)

os.mkdir(path) 創(chuàng)建目錄:

 以下兩種情況會(huì)報(bào)錯(cuò):

  •  2.要?jiǎng)?chuàng)建目錄的上級(jí)目錄不存在(a不存在,則不能創(chuàng)建b目錄)
  •  3.要?jiǎng)?chuàng)建的目錄已存在(b存在,則不能創(chuàng)建b目錄)
# coding:utf-8

import os

if __name__ == '__main__':

? ? os.mkdir('sk/a/b') ?# 在當(dāng)前sk/a 目錄中創(chuàng)建b目錄
? ? # os.mkdir(os.path.join(os.getcwd(), 'sk', 'a', 'b')) 和上面的一樣,只不過換成了絕對(duì)路徑

4.刪除文件 os.remove(path)

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? os.remove(path)
? ? 要求path是文件且存在
? ? """
? ? os.remove("sk/a/b/503.py")
? ? # os.remove(os.path.join(os.getcwd(), 'sk', 'a', 'b', '503.py')) 和上面的一樣,只不過路徑換成絕對(duì)路徑了

5.遞歸刪除空目錄 os.removedirs(path)

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? os.removedirs(path)
? ? 如果最底層目錄不是空目錄直接報(bào)錯(cuò)(s2不是空目錄則報(bào)錯(cuò))
? ? 從最第底層目錄開始刪,一直刪到最上層目錄(先刪s2,再刪s1),當(dāng)要?jiǎng)h除的目錄不是空目錄時(shí)停止(s1不是空目錄則停止)
? ? """
? ? os.removedirs('s1/s2')
? ? # os.removedirs(os.path.join(os.getcwd(), 's1', 's2')) 和上面的一樣,只不過使用了絕對(duì)路徑

6.刪除空目錄 os.rmdir(path)

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? ? os.rmdir(path)
? ? ? path不存在且不是空目錄則報(bào)錯(cuò)
? ? """
? ? os.rmdir('s1/s2')

7.創(chuàng)建多級(jí)目錄 os.makedirs(path)

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? os.makedirs(path,exists_ok=False) 創(chuàng)建多級(jí)目錄
? ? 如果path目錄已存在則報(bào)錯(cuò),可以設(shè)置exist_ok 參數(shù)來抑制錯(cuò)誤,exist_ok不等于False就不會(huì)拋出異常
? ? """
? ? os.makedirs("s1/s3")
? ? os.makedirs("s1/s3", exist_ok=True)

二、os.path 常用方法

1.路徑拼接 os.path.join(path1,path2…)

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? os.path.join(path1,path2......)
? ? 將多個(gè)路徑拼接形成新的路徑
? ? """
? ? print(os.path.join(os.getcwd(), 's1', 's2')) ?# G:\workSpace\py_d\s1\s2

2.路徑分離 os.path.split(path) 返回tuple

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? os.path.split(path) 分割路徑,返回一個(gè)tuple,由兩個(gè)元素構(gòu)成
? ? 第2個(gè)元素是path中最后一層的目錄或者文件
? ? 第1個(gè)元素是path中除去第二個(gè)元素后剩余的路徑,可以為空字符串
? ? """
? ? path = os.path.join(os.getcwd(), 's1', 's2')
? ? print(path) ?# G:\workSpace\py_d\s1\s2.my
? ? print(os.path.split(path)) ?# ('G:\\workSpace\\py_d\\s1', 's2.my')
? ? print(os.path.split('dir1')) ?# ('', 'dir1')

3.判斷是否是目錄 os.path.isdir(path)

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? os.path.is_dir(path) 判斷路徑是不是目錄,當(dāng)路徑不存在也返回False
? ? """
? ? print(os.path.isdir(os.getcwd())) ?# True
? ? print(os.path.isdir(os.path.join(os.getcwd(), 'main.py'))) ?# False 這是文件
? ? print(os.path.isdir('http://xxx.com')) ?# 不存在也返回False

4.判斷是否是文件 os.path.isfile(paht)

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? os.path.is_file(path) 判斷路徑是不是文件,當(dāng)路徑不存在也返回False
? ? """

? ? print(os.path.isfile(os.getcwd())) ?# False 這是目錄
? ? print(os.path.isfile(os.path.join(os.getcwd(), 'main.py'))) ?# True 是文件
? ? print(os.path.isfile(os.path.join(os.getcwd(), 'abc'))) ?# False 不存在也返回False

5.判斷路徑是否存在 os.path.exists(path)

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? os.path.exists(path) 判斷路徑是不是文件,當(dāng)路徑不存在也返回False
? ? """

? ? print(os.path.exists(os.path.join(os.getcwd(), 'main.py'))) ?# True main.py存在
? ? print(os.path.exists(os.path.join(os.getcwd(), 'abc.py'))) ?# False abc.py 不存在

6.判斷路徑是不是絕對(duì)路徑 os.path.isabs(path)

# coding:utf-8

import os

if __name__ == '__main__':

? ? """
? ? os.path.abs(path) 判斷路徑是不是絕對(duì)路徑,與存不存在無關(guān)
? ? """
? ? print(os.getcwd()) ?# G:\workSpace\py_d
? ? print(os.path.isabs(os.path.join(os.getcwd(), 'main.py'))) ?# True main.py存在
? ? print(os.path.isabs(os.path.join(os.getcwd(), 'abc.py'))) ? # True abc.py不存在
? ? print(os.path.isabs('a/b/c')) ?# False

到此這篇關(guān)于Python的os包與os.path模塊詳情的文章就介紹到這了,更多相關(guān)Python的os包和os.path模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • wxPython實(shí)現(xiàn)窗口用圖片做背景

    wxPython實(shí)現(xiàn)窗口用圖片做背景

    這篇文章主要為大家詳細(xì)介紹了wxPython實(shí)現(xiàn)窗口用圖片做背景,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python字符串的索引與切片

    Python字符串的索引與切片

    這篇文章主要介紹了Python字符串的索引與切片,文章圍繞主題展開詳細(xì)的相關(guān)資料,需要的小伙伴可以參考一下
    2022-04-04
  • Matplotlib實(shí)戰(zhàn)之玫瑰圖繪制詳解

    Matplotlib實(shí)戰(zhàn)之玫瑰圖繪制詳解

    南丁格爾玫瑰圖是一種用極坐標(biāo)下的柱狀圖或堆疊柱狀圖來展示數(shù)據(jù)的圖表,下面我們就來介紹一下如何使用Matplotlib繪制南丁格爾玫瑰圖,需要的可以參考下
    2023-08-08
  • python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實(shí)例

    python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實(shí)例

    這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實(shí)例,采用了類似遞歸方式建立,需要的朋友可以參考下
    2014-04-04
  • Python 實(shí)現(xiàn)簡(jiǎn)單的電話本功能

    Python 實(shí)現(xiàn)簡(jiǎn)單的電話本功能

    這篇文章主要介紹了Python 實(shí)現(xiàn)簡(jiǎn)單的電話本功能的相關(guān)資料,包括添加聯(lián)系人信息,查找姓名顯示聯(lián)系人,存儲(chǔ)聯(lián)系人到 TXT 文檔等內(nèi)容,十分的細(xì)致,有需要的小伙伴可以參考下
    2015-08-08
  • python tools實(shí)現(xiàn)視頻的每一幀提取并保存

    python tools實(shí)現(xiàn)視頻的每一幀提取并保存

    這篇文章主要為大家詳細(xì)介紹了python tools實(shí)現(xiàn)視頻的每一幀提取并保存,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Python爬蟲實(shí)現(xiàn)selenium處理iframe作用域問題

    Python爬蟲實(shí)現(xiàn)selenium處理iframe作用域問題

    這篇文章主要介紹了Python爬蟲實(shí)現(xiàn)selenium處理iframe作用域問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 使用Python處理數(shù)據(jù)集的技巧分享

    使用Python處理數(shù)據(jù)集的技巧分享

    這篇文章會(huì)從加載數(shù)據(jù)開始,一步步教大家如何格式化數(shù)據(jù)、保存數(shù)據(jù),最后還會(huì)教大家如何加載處理后的數(shù)據(jù),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • python 讀文件,然后轉(zhuǎn)化為矩陣的實(shí)例

    python 讀文件,然后轉(zhuǎn)化為矩陣的實(shí)例

    下面小編就為大家分享一篇python 讀文件,然后轉(zhuǎn)化為矩陣的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python如何使用字符打印照片

    Python如何使用字符打印照片

    這篇文章主要介紹了Python如何使用字符打印照片,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01

最新評(píng)論