Python中为何使用open()打开文件时路径设置会出现错误?

2026-05-25 03:150阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计422个文字,预计阅读时间需要2分钟。

Python中为何使用open()打开文件时路径设置会出现错误?

昨晚尝试了用Python的open()函数打开文件,代码如下:

pythondef main(): infile=open(C:\Users\Spirit\Desktop\bc.txt, 'r') data=infile.read() print(data)main()

然而,结果却显示错误:无效的参数或找不到文件。

昨晚搞鼓了一下python的open()打开文件 代码如下

def main(): infile =open("C:\Users\Spirit\Desktop\bc.txt",'r') data = infile.read() print(data) main()

然而结果总报错invaild argument 或者cant found such file ***

查找问题后 发现是由于python中的 ‘\' 是转义符号,要想输出\ 的办法有两种

1 、在\后再加\ 就是\\ 的形式

把第二行改为infile =open("C:\\Users\\Spirit\\Desktop\\bc.txt",'r') 即可

2、在路径前加个 r ,意思是按原始字符处理 。

阅读全文

本文共计422个文字,预计阅读时间需要2分钟。

Python中为何使用open()打开文件时路径设置会出现错误?

昨晚尝试了用Python的open()函数打开文件,代码如下:

pythondef main(): infile=open(C:\Users\Spirit\Desktop\bc.txt, 'r') data=infile.read() print(data)main()

然而,结果却显示错误:无效的参数或找不到文件。

昨晚搞鼓了一下python的open()打开文件 代码如下

def main(): infile =open("C:\Users\Spirit\Desktop\bc.txt",'r') data = infile.read() print(data) main()

然而结果总报错invaild argument 或者cant found such file ***

查找问题后 发现是由于python中的 ‘\' 是转义符号,要想输出\ 的办法有两种

1 、在\后再加\ 就是\\ 的形式

把第二行改为infile =open("C:\\Users\\Spirit\\Desktop\\bc.txt",'r') 即可

2、在路径前加个 r ,意思是按原始字符处理 。

阅读全文