Python 解決相對(duì)路徑問題:"No such file or directory"
如果你取相對(duì)路徑不是在主文件里,可能就會(huì)有相對(duì)路徑問題:"No such file or directory"。
因?yàn)?python 的相對(duì)路徑,相對(duì)的都是主文件。
如下目錄結(jié)構(gòu):
| -- main.py | -- conf.py | -- start.png | -- config.txt
main.py 是主文件。
conf.py 里引用 config.txt 用相對(duì)路徑。
如果用 . 或 … 相對(duì)的是 main.py,所以用 "./config.txt",相對(duì)于 main.py 是同一個(gè)目錄下。
.指當(dāng)前文件所在的文件夾,… 指當(dāng)前文件的上一級(jí)目錄。
補(bǔ)充知識(shí):解決python模塊調(diào)用時(shí)代碼中使用相對(duì)路徑訪問的文件,提示文件不存在的問題
問題分析:
在編碼過程中使用相對(duì)路徑使代碼的穩(wěn)定性更好,即使項(xiàng)目目錄發(fā)生變更,只要文件相對(duì)路徑不變,代碼依然可以穩(wěn)定運(yùn)行。但是在python代碼中使用相對(duì)路徑時(shí)會(huì)存在以下問題,示例代碼結(jié)構(gòu)如下:

其中test包中包含兩個(gè)文件first.py和user_info.txt,first.py代碼中只有一個(gè)函數(shù)read_file,用于讀取user_info.txt文件第一行的內(nèi)容,并打印結(jié)果,讀取文件使用相對(duì)路徑,代碼如下:
import os
print("當(dāng)前路徑 -> %s" %os.getcwd())
def read_file() :
with open("user_info.txt" , encoding = 'utf-8') as f_obj :
content = f_obj.readline()
print("文件內(nèi)容 -> %s" %content)
if __name__ == '__main__' :
read_file()
first.py程序代碼執(zhí)行結(jié)果如下:
當(dāng)前路徑 -> E:\程序\python代碼\PythonDataAnalysis\Demo\test
文件內(nèi)容 -> hello python !!!
與test在同一目錄下存在一個(gè)second.py文件,在這個(gè)文件中調(diào)用first.py文件中的read_file方法讀取user_info.txt文件,代碼如下:
from test import first
first.read_file()
second.py程序執(zhí)行結(jié)果如下:
當(dāng)前路徑 -> E:\程序\python代碼\PythonDataAnalysis\Demo
File "E:/程序/python代碼/PythonDataAnalysis/Demo/second.py", line 8, in <module>
first.read_file()
File "E:\程序\python代碼\PythonDataAnalysis\Demo\test\first.py", line 10, in read_file
with open("user_info.txt" , encoding = 'utf-8') as f_obj :
FileNotFoundError: [Errno 2] No such fileor directory: 'user_info.txt'
以上信息提示user_info.txt 文件不存在,查看os.getcwd() 函數(shù)輸出的當(dāng)前路徑會(huì)發(fā)現(xiàn),當(dāng)前路徑是 XXX/Demo,而不是上一次單獨(dú)執(zhí)行first.py 文件時(shí)的 XXX/Demo/test了,所以程序報(bào)錯(cuò)文件不存在的根本原因是因?yàn)楫?dāng)前路徑變了,導(dǎo)致代碼中的由相對(duì)路徑構(gòu)成的絕對(duì)路徑發(fā)生了變化。
解決方法:
對(duì)于這種問題,只需要在使用相對(duì)路徑進(jìn)行文件訪問的模塊中加入以下代碼即可(加粗內(nèi)容),修改后的first.py代碼如下:
import os
print("當(dāng)前路徑 -> %s" %os.getcwd())
current_path = os.path.dirname(__file__)
def read_file() :
with open(current_path + "/user_info.txt" , encoding = 'utf-8') as f_obj :
content = f_obj.readline()
print("文件內(nèi)容 -> %s" %content)
if __name__ == '__main__' :
read_file()
first.py 程序執(zhí)行結(jié)果如下:
當(dāng)前路徑 -> E:\程序\python代碼\PythonDataAnalysis\Demo\test
current_path -> E:/程序/python代碼/PythonDataAnalysis/Demo/test
文件內(nèi)容 -> hello python !!!
second.py代碼不變,second.py代碼執(zhí)行結(jié)果如下:
當(dāng)前路徑 -> E:\程序\python代碼\PythonDataAnalysis\Demo
current_path -> E:\程序\python代碼\PythonDataAnalysis\Demo\test
文件內(nèi)容 -> hello python !!!
由以上執(zhí)行結(jié)果可以發(fā)現(xiàn),雖然first.py和second.py代碼執(zhí)行時(shí)os.getcwd()函數(shù)的輸出結(jié)果還是不一致,但是current_path = os.path.dirname(__file__)
代碼得到的current_path路徑是相同的,current_path就是first.py文件所處的路徑,然后再由current_path 和user_info.txt 組成的文件絕對(duì)路徑則是固定的,這樣就可以確保在進(jìn)行模塊導(dǎo)入時(shí),模塊中使用相對(duì)路徑進(jìn)行訪問的文件不會(huì)出錯(cuò)。
以上這篇Python 解決相對(duì)路徑問題:"No such file or directory"就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于np.arange與np.linspace細(xì)微區(qū)別(數(shù)據(jù)溢出問題)
這篇文章主要介紹了基于np.arange與np.linspace細(xì)微區(qū)別(數(shù)據(jù)溢出問題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
解決python訓(xùn)練模型報(bào)錯(cuò):BrokenPipeError:?[Errno?32]?Broken?pipe
這篇文章主要介紹了解決python訓(xùn)練模型報(bào)錯(cuò):BrokenPipeError:?[Errno?32]?Broken?pipe問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Python實(shí)現(xiàn)中文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)中文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字的方法,涉及Python字符串遍歷、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
利用Python?Matlab繪制曲線圖的簡單實(shí)例
們經(jīng)常會(huì)遇到這種情況,有一個(gè)數(shù)學(xué)函數(shù),我們希望了解他的圖像,這個(gè)時(shí)候使用python 的matplotlib就可以幫助我們,下面這篇文章主要介紹了利用Python?Matlab繪制曲線圖的相關(guān)資料,需要的朋友可以參考下2021-12-12
Python?Celery定時(shí)任務(wù)詳細(xì)講解
這篇文章主要介紹了Python?Celery定時(shí)任務(wù)詳細(xì)講解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08

