in Education by
What is the best way to open a file as reading/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 to the problem. I am curious about the other stuff, but all I need is a nice way to do the opening part. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
When we use the following approach it has an advantage which is, the file is properly closed at the block's end, even if an exception is raised on the way. It's equivalent to try-finally but much shorter. with open("file.dat","a+") as f: f.write(...) ... When we use a+ it opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

Related questions

0 votes
    I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I ... to this problem look like? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters? file. ... be a standard way. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Is there a way to get functionality similar to mkdir -p on the shell from within Python. I am looking for a ... has already written it? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to find all the files in directory with .txt extension in Python? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I looked into the Python os interface, but was unable to locate a method to move a file. How ... mv source_files destination_folder Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    This is my code: import SimpleITK as sitk reader = sitk.ImageFileReader() reader.SetFileName(filePath) reader.ReadImageInformation() img ... meta = { "a": reader.GetMetaData('0'),...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some ... my original project checkout. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    in the source directory, i have some binary files named 1.bin, 2.bin, 3.bin, etc. In the dest ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    How can I find where my httpd.conf file is located? I am running an Ubuntu Linux server from the Amazon Web ... find my Apache config. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    This is my code it's just starting the scan but it is not completing ,where is the error in it. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I'm curious about manipulating time in Python. I can get the (last modified) age of a file using ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
...