in Education by
I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I somehow can combine the multiple open calls with the with a statement: try: with open('a', 'w') as a and open('b', 'w') as b: do_something() except IOError as e: print 'Operation failed: %s' % e.strerror If that's not possible, what would an elegant solution to this problem look like? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Actually, you are following the correct way to open multiple files in Python in the only thing you need to do is, replace ‘and’ with ‘,’ and you are done with your answer:-. try: with open('a', 'w') as a, open('b', 'w') as b: do_something() except IOError as e: print('Operation failed: %s' % e.strerror) To know more about this you can have a look at the following video:-

Related questions

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
    What is the best way to open a file as reading/write if it exists, or if it does not, then create it and ... to do the opening part. 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
    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
    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
    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
    webbrowser.open('https://api.WhatsApp.com/send?phone=number') I want to send WhatsApp messages to numbers without ... in this link. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    How do I import other files in Python? How exactly can I import a specific python file like import file.py? How ... gap from extra.py? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me what jobs can I get with Python? Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    What is the problem Here ? I got an error shows ValueError: too many values to unpack This code ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I've been given to understand that Python is an interpreted language... However, when I look at my Python source ... do these come in? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I want to save files for each result from the loop. For example, I wrote the code but it only saves one file ... do I modify my code? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Do I need coding skills for Data Science using Python? Select the correct answer from above options...
asked Jan 17, 2022 in Education by JackTerrance
0 votes
    I have drawn an image in the device context using python and I want to move it smoothly/animate either ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
...