我试图用这一行打开一个不存在的文件:
x = open("~/tweetly/auth", 'w+')
如果存在应该打开它,然后删除内容以开始写入。 如果它不存在,它应该创建它...对吧?
事实并非如此。我收到此错误。
IOError: [Errno 2] No such file or directory: '~/tweetly/auth'
想法?
请您参考如下方法:
主目录的 ~ 别名是一个 shell-ism(shell 为你做的事情),不是你可以用 Python open 命令使用的东西:
pax:~$ cd ~
pax:~$ ls qq.s
qq.s
pax:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> open("~/qq.s")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '~/qq.s'
>>> open("./qq.s")
<open file './qq.s', mode 'r' at 0xb7359e38>
>>> _
