新人求教,关于Python用open()找不到文件的问题。。
小弟这几天自己在看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'
>>>
[解决办法]
我这里是可以的。然后也故意弄个错的给你看了:
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'
>>>