The question you tried to load has been merged into the question below

Asked • 05/04/19

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? 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. 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. UPDATE: 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: > IOError: no such file or directory.

2 Answers By Expert Tutors

By:

Cheryl F. answered • 05/04/19

Tutor
New to Wyzant

Scientific Programming is Science aided by Software

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.