新人求教,关于Python用open()找不到文件的问题。。

Samerol_Jomior 2013-08-08 05:18:38
小弟这几天自己在看python。。今天看到open()这个,倒腾好久都没搞好。。希望前辈们指教一下。。我已经创建好test.txt的文件了。。为什么找不到呢。。
>>> the_file = open('test.txt')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
the_file = open('test.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
>>> the_file = open('D:\\Program Files\\Python\\test.txt')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
the_file = open('D:\\Program Files\\Python\\test.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\Program Files\\Python\\test.txt'
>>>
...全文
3629 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Samerol_Jomior 2013-08-08
  • 打赏
  • 举报
回复
引用 2 楼 crifan 的回复:
我这里是可以的。然后也故意弄个错的给你看了:
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> the_file = open("D:\\Program Files\\Python\\test.txt");
>>> print the_file
<open file 'D:\Program Files\Python\test.txt', mode 'r' at 0x0000000002FAA270>
>>> notExist_file = open("D:\\Program Files\\Python\\notFound.txt");

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    notExist_file = open("D:\\Program Files\\Python\\notFound.txt");
IOError: [Errno 2] No such file or directory: 'D:\\Program Files\\Python\\notFound.txt'
>>> 
所以: 1.猜测你的python是不是安装的有问题。 重新安装一下试试。 2.你的python是什么版本的???
我的是3.3的那个,我也觉得我的这个有问题,有时候在按backspace键的时候会出现小方块。。但是换了输入法又好了。。我好几次卸载安装,都是这种情况。。
crifan 2013-08-08
  • 打赏
  • 举报
回复
我这里是可以的。然后也故意弄个错的给你看了:
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> the_file = open("D:\\Program Files\\Python\\test.txt");
>>> print the_file
<open file 'D:\Program Files\Python\test.txt', mode 'r' at 0x0000000002FAA270>
>>> notExist_file = open("D:\\Program Files\\Python\\notFound.txt");

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    notExist_file = open("D:\\Program Files\\Python\\notFound.txt");
IOError: [Errno 2] No such file or directory: 'D:\\Program Files\\Python\\notFound.txt'
>>> 
所以: 1.猜测你的python是不是安装的有问题。 重新安装一下试试。 2.你的python是什么版本的???
叶恭介叶恭介 2013-08-08
  • 打赏
  • 举报
回复
the_file = open('D:\\Program Files\\Python\\test.txt')..好像不用\\,直接\

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧