若是不存在,則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
相關文章
相關標籤/搜索