[Python学习]错误篇二:切换当前工作目录时出错——FileNotFoundError: [WinError 3] 系统找不到指定的路径
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这段路径相同就不用写出)。
(折腾完了)
[Python学习]错误篇二:切换当前工作目录时出错——FileNotFoundError: [WinError 3] 系统找不到指定的路径的更多相关文章
- python setup.py install 报错:error: [WinError 3] 系统找不到指定的路径。: 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\PlatformSDK\\lib
Outline 在通过 setup.py 安装python模块时,遇到了以下报错: # 执行 python setup.py install # 报错: error: [WinError 3] 系统找 ...
- python FileNotFoundError: [WinError 2] 系统找不到指定的文件。
D:\Test\TestSWP\Scripts\python.exe D:/Test/SWP/TestSWP/chorme/chorme.pyTraceback (most recent call l ...
- [Python学习]错误篇一
REFERENCE:<Head First Python> ID:我的第一篇[Python学习] BIRTHDAY:2019.7.6 EXPERIENCE_SHARING:两个程序错误类型 ...
- python 【winerror2】系统找不到指定的路径
# _*_ coding:utf-8_*_from selenium import webdriver driver = webdriver.Firefox()driver.get("htt ...
- 错误/异常:java.io.FileNotFoundException: .\src\db.properties (系统找不到指定的路径。);的解决方法
1.异常视图 2.解决方法 与之相关的部分代码: static{ try { //读取db.properties Properties props = new Properties(); FileIn ...
- 解决java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, 系统找不到指定的文件 的错误
一.外部环境: 系统环境:Windows 8 磁盘分区:只有C盘 开发环境:IntelliJ IDEA Community Edition 2016.1.3(64) 执行代码:rdd.saveAsTe ...
- 使用ImageIO.write上传二维码文件时候,提示系统找不到指定路径
报错如图所示: java.io.FileNotFoundException: E:\SF\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtp ...
- Python学习笔记(二)——列表
Python学习笔记(二)--列表 Python中的列表可以存放任何数据类型 >>> list1 = ['Hello','this','is','GUN',123,['I','Lov ...
- 服务器应用程序不可用,由于无法创建应用程序域,因此未能执行请求。错误: 0x80070002 系统找不到指定的文件。
使用360更新网站补丁导致.net2.0环境报错问题现象:服务器应用程序不可用查看日志:出现由于无法创建应用程序域,因此未能执行请求.错误: 0x80070002 系统找不到指定的文件. 搜索定位:罪 ...
随机推荐
- Linux技术学习路线图
- DB First .edmx
DB First查看Entity相互关系.edmx 图表 .edmx源代码——xml文件右键,打开方式 xml内容 详细查看DB:.edmx—Model Browser(模型浏 ...
- 微信小程序把玩(二十八)image组件
原文:微信小程序把玩(二十八)image组件 image组件也是一个程序不可缺少的,可以这样说一个app中image组件随处可以看到,一般 image有两种加载方式第一种是网络图片第二种是本地图片资源 ...
- Android零基础入门第28节:轻松掌握RelativeLayout相对布局
原文:Android零基础入门第28节:轻松掌握RelativeLayout相对布局 在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局. ...
- CLSRSC-400: A system reboot is required to continue installing.
I try to install oracle database 12c RAC on the RedHat 7.3,when I execute the script '/u01/app/12.2. ...
- [机器学习]SVM原理
SVM是机器学习中神一般的存在,虽然自深度学习以来有被拉下神坛的趋势,但不得不说SVM在这个领域有着举足轻重的地位.本文从Hard SVM 到 Dual Hard SVM再引进Kernel Trick ...
- NUMA 架构
NUMA架构的CPU -- 你真的用好了么? - http://cenalulu.github.io/linux/numa/ SQL Server 如何支持 NUMA - https://docs.m ...
- Mono 4.0 发布,开源跨平台 .Net 框架
快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中. <HTML开发Mac OS App 视频教程> 土豆网同步更新:http: ...
- qt中文编码(好多方法)
qt中文编码 来源:http://www.cublog.cn/u1/59481/showart_1947231.html 前些日子,被编码折磨了一段时间,总结一下Qt中的编码. [Qt 编码简单实验] ...
- Notepad2(C语言+Windows消息写的,24592行代码)
C语言+Windows消息写的,24592行代码 http://www.flos-freeware.ch/