REFERENCE:《Head First Python》
ID:我的第二篇[Python學習]
BIRTHDAY:2019.7.13
EXPERIENCE_SHARING:解決切換當前工作目錄時出現的錯誤——FileNotFoundError
1、錯誤類型
FileNotFoundError: [WinError 3] 系統找不到指定的路徑。: '../HeadFirstPython/chapter3'
在文件夾D:\0tempt,新建了文件夾 HeadFirstPython,其包含子文件夾chapter3。
試圖更改 當前工作目錄為包含數據文件的文件夾,卻出錯了……
>>> import os #從標准庫導入"os" >>> os.getcwd() 'D:\\Python37' #當前工作目錄 >>> os.chdir('../HeadFirstPython/chapter3') #切換為包含數據文件的文件夾 Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> os.chdir('../HeadFirstPython/chapter3') FileNotFoundError: [WinError 3] 系統找不到指定的路徑。: '../HeadFirstPython/chapter3'
先把書上的例子放出來:
>>>import os >>> os.getcwd () ' /Users/barryp/Documents ' >>> os. chdir('. . /HeadFirstPython/ chapter3') >>> os. getcwd () ' /Users/barryp/HeadFirs tPython/ chapter3'
對比一下,突然有新發現:
#當前工作目錄
我的—— 'D:\\Python37'
書上的—— ' /Users/barryp/Documents '
沒錯,斜杠符號的不同,'/ '和' \',有不同嗎?來試試看——
接下來的一串,是不斷探索的結果:
(1)把'/ ' 換成了 ' \'——
還是有錯……
>>> os.chdir('..\HeadFirstPython\chapter3') Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> os.chdir('..\HeadFirstPython\chapter3') FileNotFoundError: [WinError 3] 系統找不到指定的路徑。: '..\\HeadFirstPython\\chapter3'
(2) 是路徑不夠完整?那寫完整的試試——
還是有錯……錯誤類型改變了
ValueError: chdir: embedded null character in path 意思是:嵌入了無效字符
>>> os.chdir('D:\0tempt\HeadFirstPython\chapter3') Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> os.chdir('D:\0tempt\HeadFirstPython\chapter3') ValueError: chdir: embedded null character in path
(3)再觀察一下——
路徑 D:\0tempt\HeadFirstPython\chapter3,是在電腦文件夾搜索欄直接復制過來的,相比以下路徑
>>> os.getcwd() 'D:\\Python37' #當前工作目錄
斜杠少了一個……
那試着都增加一個——
>>> os.chdir('D:\\0tempt\\HeadFirstPython\\chapter3') >>> os.getcwd() 'D:\\0tempt\\HeadFirstPython\\chapter3'
Great! You make it~~
切換當前工作目錄操作完整的代碼:
>>> import os >>> os.getcwd() 'D:\\Python37' >>> os.chdir('D:\\0tempt\\HeadFirstPython\\chapter3') >>> os.getcwd() 'D:\\0tempt\\HeadFirstPython\\chapter3'
(4)再來探究一下:
1、書上的例子中,路徑的寫法 ——'/ '和' \',有什么不同嗎?
2、路徑的寫法規則
為什么在自己電腦磁盤的文件夾搜索欄里復制的文件路徑是單斜杠
D:\0tempt\HeadFirstPython\chapter3
而代碼中是雙斜杠
'D:\\0tempt\\HeadFirstPython\\chapter3'
>>> os.chdir('D:\\0tempt\\HeadFirstPython\\chapter3') >>> os.getcwd() 'D:\\0tempt\\HeadFirstPython\\chapter3'
>>> os.chdir('D://0tempt//HeadFirstPython//chapter3') #換成'//'——同樣也沒錯 >>> os.getcwd() 'D:\\0tempt\\HeadFirstPython\\chapter3' >>> os.chdir('D:\0tempt\HeadFirstPython\chapter3') #換成'\'——出錯了——這種寫法錯誤 Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> os.chdir('D:\0tempt\HeadFirstPython\chapter3') ValueError: chdir: embedded null character in path >>> os.chdir('D:/0tempt/HeadFirstPython/chapter3') #換成'/'——同樣也沒錯 >>> os.getcwd() 'D:\\0tempt\\HeadFirstPython\\chapter3'
'D:\\0tempt\\HeadFirstPython\\chapter3'
'D://0tempt//HeadFirstPython//chapter3'
'D:/0tempt/HeadFirstPython/chapter3'
三種路徑的寫法都OK
【總結】
書上的例子:
>>>import os
>>> os.getcwd () ' /Users/barryp/Documents ' >>> os. chdir('. . /HeadFirstPython/ chapter3') >>> os. getcwd () ' /Users/barryp/HeadFirstPython/ chapter3'
'. . /HeadFirstPython/ chapter3'
使用的是相對路徑,在代碼中會出錯。原因是什么,學識尚淺,待解決,也請走過路過的大佬指教~
代碼修改時,使用的是絕對路徑,運行無誤。
————————————————————————————————————————————————————————--
咳咳……剛剛查資料時突然解決了這個問題——
../ 表示的是表示上一級目錄(也可以理解為 上一級文件夾,上行一個文件夾;
拿上面書上的例子來解釋,就是文件夾HeadFirstPython/ chapter3往上一級,就到達目錄文件夾/Users/barryp)
./ 表示的是當前目錄
來試驗一下:
1、將文件夾 HeadFirstPython(包含子文件夾chapter3,子文件夾里存放文件sketch.txt)放在Python安裝目錄 D:\Python37\0PRACTICES。
>>> import os >>> os.getcwd() 'D:\\Python37' >>> os.chdir('./0PRACTICES/HeadFirstPython/chapter3') >>> os.getcwd() 'D:\\Python37\\0PRACTICES\\HeadFirstPython\\chapter3'
因為 文件夾 HeadFirstPython是放在Python默認的當前工作文件夾 'D:\\Python37'中,
故 當前目錄 即 'D:\\Python37',代碼中用“./”,表示“在當前目錄”。
>>> os.chdir('./0PRACTICES/HeadFirstPython/chapter3')
2、將文件夾 HeadFirstPython(包含子文件夾chapter3,子文件夾里存放文件sketch.txt)放在文件夾 D:\0tempt。
>>> import os >>> os.getcwd() 'D:\\Python37' >>> os.chdir('../HeadFirstPython/chapter3') Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> os.chdir('../HeadFirstPython/chapter3') FileNotFoundError: [WinError 3] 系統找不到指定的路徑。: '../HeadFirstPython/chapter3' >>> os.chdir('/0tempt/HeadFirstPython/chapter3') >>> os.getcwd() 'D:\\0tempt\\HeadFirstPython\\chapter3' >>>
在這個例子中,文件夾 HeadFirstPython不是放在Python默認的當前工作文件夾 'D:\\Python37'中,而是同根磁盤的D:\0tempt,
故這時的當前目錄不是'D:\\Python37',而是“D:”,代碼中未用“./”,直接省略根目錄“D:”。
>>> os.chdir('/0tempt/HeadFirstPython/chapter3')
——————————————————————————————————————————————————
/0tempt/HeadFirstPython/chapter3
這種路徑表示是正確的,因為這是 相對路徑,文件夾0tempt和文件夾Python37 根目錄都是D盤。所以省略了根目錄D,也是對的。
上面驗證了,以下三種路徑都ok
'D:\\0tempt\\HeadFirstPython\\chapter3'
'D://0tempt//HeadFirstPython//chapter3'
'D:/0tempt/HeadFirstPython/chapter3'
三種路徑的寫法都OK
那省略一下根目錄:(又是吃飽了折騰一下)
>>> os.chdir('\\0tempt\\HeadFirstPython\\chapter3') >>> os.getcwd() 'D:\\0tempt\\HeadFirstPython\\chapter3' >>> os.chdir('//0tempt//HeadFirstPython//chapter3') Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> os.chdir('//0tempt//HeadFirstPython//chapter3') FileNotFoundError: [WinError 53] 找不到網絡路徑。: '//0tempt//HeadFirstPython//chapter3' >>> os.chdir('/0tempt/HeadFirstPython/chapter3') >>> os.getcwd() 'D:\\0tempt\\HeadFirstPython\\chapter3'
這里再補充一下絕對路徑和相對路徑的知識:
絕對路徑:
即從最大的根目錄開始表示,一直到該文件名。
相對路徑:
即該文件自己相對於目標(另一個文件)位置。不論將這些文件放到哪里,只要他們的相對關系沒有變,就不會出錯。
另外通常使用“../”來表示上一級目錄(上行一個文件夾),“../../”表示上上級的目錄(上行兩個文件夾)(這里的“上行”,可以理解為向着根目錄文件夾方向走),
以此類推。 ./表示“當前目錄”。
舉例:
1、
C盤A文件夾 下有兩個 文件X 和 文件Y
表示文件X的位置(即路徑),兩種表示方法:
C:\A\X
這就是 絕對路徑,指明X文件在C盤A文件夾下,從最大的根目錄C盤開始表示出來
X
這就是 相對路徑,因為X文件和Y文件都在C:\A下,所以它們的路徑前面"C:\A"都是一樣,就不用表示出來.
C盤A文件夾 有個X文件,還有一個B文件夾,而B文件夾下有個Y文件.
則文件X和文件Y 絕對路徑 分別為:
C:\A\X
C:\A\B\Y
如果讓X文件來表示Y文件的路徑
絕對路徑: C:\A\B\Y
相對路徑: B\Y (因為X文件和Y文件前面的C:\A這段路徑相同就不用寫出)。
(折騰完了)