之前用python的时候,一直根据提示就可以获取到文件,但是后来发现,这样是有错误的。比如:
1.
>>> f=open('.\mytest\a.txt')
Traceback (most recent call last):
IOError: [Errno 22] invalid mode ('r') or filename: '.\\mytest\x07.txt'
【释】我是根据IDE的提示用反斜线打出来的路径,但是会提示IOError。
2.
>>> f=open('.\\mytest\\a.txt')
>>> f.read()
>>> f.close()
【释】双反斜线 成功!
3.
>>> f=open('./mytest/a.txt')
>>> f.read()
'aaa'
>>> f.close()
【释】单斜线,成功!
4.
>>> f=open('.//mytest//a.txt','rb')
>>> f
>>> f.read()
'aaa'
>>> f.close()
【释】咦,双斜线也可以成功的!
以上操作均在windows上。
原创所有,转载请附加链接,谢谢!