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”都是一样,就不用表示出来.

 

2、

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这段路径相同就不用写出)。

 

(折腾完了)

版权声明:本文为bigorangecc原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/bigorangecc/p/11179863.html