若是不存在,则Python中的open()不会建立文件 - open() in Python does not create a file if it doesn't exist

问题:

What is the best way to open a file as read/write if it exists, or if it does not, then create it and open it as read/write? 若是文件以读/写方式打开,或者以不存在的方式建立,而后以读/写方式打开,最好的方法是什么? From what I read, file = open('myfile.dat', 'rw') should do this, right? 根据个人阅读, file = open('myfile.dat', 'rw')应该这样作,对吗? this

It is not working for me (Python 2.6.2) and I'm wondering if it is a version problem, or not supposed to work like that or what. 它对我不起做用(Python 2.6.2),我想知道这是不是版本问题,或者不该该那样工做或作什么。 spa

The bottom line is, I just need a solution for the problem. 最重要的是,我只须要解决这个问题。 I am curious about the other stuff, but all I need is a nice way to do the opening part. 我对其余东西很好奇,可是我所须要的只是作开始部分的好方法。 .net

The enclosing directory was writeable by user and group, not other (I'm on a Linux system... so permissions 775 in other words), and the exact error was: 封闭目录可由用户和组而非其余用户(我在Linux系统上...所以权限775)可写,确切的错误是: code

IOError: no such file or directory. IOError:没有这样的文件或目录。 get


解决方案:

参考一: https://stackoom.com/question/CRty/若是不存在-则Python中的open-不会建立文件
参考二: https://oldbug.net/q/CRty/open-in-Python-does-not-create-a-file-if-it-doesn-t-exist
相关文章
相关标签/搜索