Python實(shí)現(xiàn)迅速獲取文件的路徑
文件準(zhǔn)備

如圖我要獲取get_path的絕對(duì)路徑,以及下方MP3文件的絕對(duì)路徑
代碼準(zhǔn)備
1.獲取當(dāng)前工作目錄的絕對(duì)路徑
import os
# 獲取當(dāng)前工作目錄的絕對(duì)路徑
current_directory = os.getcwd()
print("當(dāng)前工作目錄的絕對(duì)路徑是:", current_directory)
運(yùn)行結(jié)果

2.獲取指定文件的絕對(duì)路徑
import os
# 獲取當(dāng)前工作目錄的絕對(duì)路徑
current_directory = os.getcwd()
filepath="006-get_path\mp3\Swag _ Miyauchi _ Audio.mp3"
print("當(dāng)前工作目錄的絕對(duì)路徑是:", current_directory+filepath)
運(yùn)行結(jié)果

我要獲得MP3下的swag……文件的路徑,那么我先打印出絕對(duì)路徑current_dictory然后加上 \006-get_path\mp3\Swag _ Miyauchi _ Audio.mp3即可

打印成功
應(yīng)用
# 使用示例
if __name__ == "__main__":
# 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式
input_format = "mp3"
output_format = "wav"
# 單文件轉(zhuǎn)換示例
input_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3"
output_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav"
converter = AudioConverter(input_format, output_format)
converter.convert(input_file_path, output_file_path)比如此處代碼我們要插入冗長的路徑,這樣如果是多文件的話

將會(huì)十分的麻煩
所以我們修改代碼為
import os
# 獲取當(dāng)前工作目錄的絕對(duì)路徑
current_directory = os.getcwd()
filepath1="/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3"
filepath2="/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav"
filefullpath1=current_directory+filepath1
filefullpath2=current_directory+filepath2
print(filefullpath1)
print(filefullpath2)
# 使用示例
if __name__ == "__main__":
# 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式
input_format = "mp3"
output_format = "wav"
# 單文件轉(zhuǎn)換示例
input_file_path = filefullpath1
output_file_path = filefullpath2
converter = AudioConverter(input_format, output_format)
converter.convert(input_file_path, output_file_path)使用os.getcwd()獲取絕對(duì)路徑,免去繁雜的路徑,這樣我們就能快速得到文件的路徑了
需要注意的是,我們獲取的是文件的絕對(duì)路徑

為了得到Joel Adams - Please Don't Go (Official Music Video).mp3的路徑,我們需要加上
“/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3”
具體的代碼就是這樣的
import os # 獲取當(dāng)前工作目錄的絕對(duì)路徑 current_directory = os.getcwd() filepath1="/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3" filefullpath1=current_directory+filepath1
獲取絕對(duì)路徑后將兩個(gè)路徑相加即可
僅僅是兩個(gè)文件的路徑也許不明顯,但是如果是成白上千的文件,那么如此獲得的路徑將會(huì)節(jié)約許許多多的時(shí)間
到此這篇關(guān)于Python實(shí)現(xiàn)迅速獲取文件的路徑的文章就介紹到這了,更多相關(guān)Python獲取文件路徑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python圖像處理之直線和曲線的擬合與繪制【curve_fit()應(yīng)用】
這篇文章主要介紹了Python圖像處理之直線和曲線的擬合與繪制,結(jié)合實(shí)例形式分析了Python曲線擬合相關(guān)函數(shù)curve_fit()的使用技巧,需要的朋友可以參考下2018-12-12
Python實(shí)現(xiàn)文本文件拆分寫入到多個(gè)文本文件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)文本文件拆分寫入到多個(gè)文本文件的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
From CSV to SQLite3 by python 導(dǎo)入csv到sqlite實(shí)例
今天小編就為大家分享一篇From CSV to SQLite3 by python 導(dǎo)入csv到sqlite實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python+PyQt5實(shí)現(xiàn)自動(dòng)化任務(wù)管理
這篇文章主要為大家詳細(xì)介紹了如何通過PyQt5構(gòu)建圖形界面,使用Python實(shí)現(xiàn)了一個(gè)自動(dòng)化任務(wù)管理系統(tǒng),感興趣的小伙伴可以參考一下2025-04-04
教你使用Python的pygame模塊實(shí)現(xiàn)拼圖游戲
pygame模塊是一個(gè)可以跨平臺(tái)的模塊,其設(shè)計(jì)目的就是為電子游戲而設(shè)計(jì),能夠支持圖片和聲音,下面這篇文章主要給給大家介紹了關(guān)于使用Python的pygame模塊實(shí)現(xiàn)拼圖游戲的相關(guān)資料,需要的朋友可以參考下2022-07-07
python實(shí)現(xiàn)sm2和sm4國密(國家商用密碼)算法的示例
這篇文章主要介紹了python實(shí)現(xiàn)sm2和sm4國密(國家商用密碼)算法的示例,幫助大家使用python加密文件,感興趣的朋友可以了解下2020-09-09
Python使用sqlalchemy模塊連接數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python使用sqlalchemy模塊連接數(shù)據(jù)庫操作,結(jié)合實(shí)例形式分析了sqlalchemy模塊的安裝及連接、調(diào)用數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2019-03-03

