Python open or create file using with clause [duplicate]












1
















This question already has an answer here:




  • python open built-in function: difference between modes a, a+, w, w+, and r+?

    6 answers




I have the following code, but it doesn't work if the file doesn't exist:



def log(self, action, data):
import json
with open('ReservationsManagerApp/logs/'+data['booking']+'.json', 'r+') as outfile:
log_data = {
'timestamp': str(datetime.today()),
'action': action,
'data': data
}
json.dump(log_data, outfile)


I want the method to create the file if it doesn't exist, but all the examples I have found don't explain how to do it using with clause, they just use try: open.
How can I instruct with clause to create the file if it doesn't exist?










share|improve this question













marked as duplicate by Matthieu Brucher, stovfl, Community Nov 27 '18 at 19:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Have you tried w+ instead of r+?

    – Matthieu Brucher
    Nov 27 '18 at 17:52













  • You can just next the with inside the try, these are two separate, composable constructs. However, there may be a mode that allows you to not have to use try-except

    – juanpa.arrivillaga
    Nov 27 '18 at 17:53








  • 2





    @MatthieuBrucher w+ will truncate if it exists. Likely, the OP wants 'a' mode, i.e. "append"

    – juanpa.arrivillaga
    Nov 27 '18 at 17:54








  • 1





    Oh yes, that's true. Anyway all these modes are easily retrieved from the open documentation, i.e. the question is moot, open does create the file if required.

    – Matthieu Brucher
    Nov 27 '18 at 17:55








  • 1





    For reference: docs.python.org/3/library/functions.html#open

    – Matthieu Brucher
    Nov 27 '18 at 17:56
















1
















This question already has an answer here:




  • python open built-in function: difference between modes a, a+, w, w+, and r+?

    6 answers




I have the following code, but it doesn't work if the file doesn't exist:



def log(self, action, data):
import json
with open('ReservationsManagerApp/logs/'+data['booking']+'.json', 'r+') as outfile:
log_data = {
'timestamp': str(datetime.today()),
'action': action,
'data': data
}
json.dump(log_data, outfile)


I want the method to create the file if it doesn't exist, but all the examples I have found don't explain how to do it using with clause, they just use try: open.
How can I instruct with clause to create the file if it doesn't exist?










share|improve this question













marked as duplicate by Matthieu Brucher, stovfl, Community Nov 27 '18 at 19:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Have you tried w+ instead of r+?

    – Matthieu Brucher
    Nov 27 '18 at 17:52













  • You can just next the with inside the try, these are two separate, composable constructs. However, there may be a mode that allows you to not have to use try-except

    – juanpa.arrivillaga
    Nov 27 '18 at 17:53








  • 2





    @MatthieuBrucher w+ will truncate if it exists. Likely, the OP wants 'a' mode, i.e. "append"

    – juanpa.arrivillaga
    Nov 27 '18 at 17:54








  • 1





    Oh yes, that's true. Anyway all these modes are easily retrieved from the open documentation, i.e. the question is moot, open does create the file if required.

    – Matthieu Brucher
    Nov 27 '18 at 17:55








  • 1





    For reference: docs.python.org/3/library/functions.html#open

    – Matthieu Brucher
    Nov 27 '18 at 17:56














1












1








1









This question already has an answer here:




  • python open built-in function: difference between modes a, a+, w, w+, and r+?

    6 answers




I have the following code, but it doesn't work if the file doesn't exist:



def log(self, action, data):
import json
with open('ReservationsManagerApp/logs/'+data['booking']+'.json', 'r+') as outfile:
log_data = {
'timestamp': str(datetime.today()),
'action': action,
'data': data
}
json.dump(log_data, outfile)


I want the method to create the file if it doesn't exist, but all the examples I have found don't explain how to do it using with clause, they just use try: open.
How can I instruct with clause to create the file if it doesn't exist?










share|improve this question















This question already has an answer here:




  • python open built-in function: difference between modes a, a+, w, w+, and r+?

    6 answers




I have the following code, but it doesn't work if the file doesn't exist:



def log(self, action, data):
import json
with open('ReservationsManagerApp/logs/'+data['booking']+'.json', 'r+') as outfile:
log_data = {
'timestamp': str(datetime.today()),
'action': action,
'data': data
}
json.dump(log_data, outfile)


I want the method to create the file if it doesn't exist, but all the examples I have found don't explain how to do it using with clause, they just use try: open.
How can I instruct with clause to create the file if it doesn't exist?





This question already has an answer here:




  • python open built-in function: difference between modes a, a+, w, w+, and r+?

    6 answers








python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 17:51









Hugo Luis Villalobos CantoHugo Luis Villalobos Canto

832516




832516




marked as duplicate by Matthieu Brucher, stovfl, Community Nov 27 '18 at 19:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Matthieu Brucher, stovfl, Community Nov 27 '18 at 19:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Have you tried w+ instead of r+?

    – Matthieu Brucher
    Nov 27 '18 at 17:52













  • You can just next the with inside the try, these are two separate, composable constructs. However, there may be a mode that allows you to not have to use try-except

    – juanpa.arrivillaga
    Nov 27 '18 at 17:53








  • 2





    @MatthieuBrucher w+ will truncate if it exists. Likely, the OP wants 'a' mode, i.e. "append"

    – juanpa.arrivillaga
    Nov 27 '18 at 17:54








  • 1





    Oh yes, that's true. Anyway all these modes are easily retrieved from the open documentation, i.e. the question is moot, open does create the file if required.

    – Matthieu Brucher
    Nov 27 '18 at 17:55








  • 1





    For reference: docs.python.org/3/library/functions.html#open

    – Matthieu Brucher
    Nov 27 '18 at 17:56



















  • Have you tried w+ instead of r+?

    – Matthieu Brucher
    Nov 27 '18 at 17:52













  • You can just next the with inside the try, these are two separate, composable constructs. However, there may be a mode that allows you to not have to use try-except

    – juanpa.arrivillaga
    Nov 27 '18 at 17:53








  • 2





    @MatthieuBrucher w+ will truncate if it exists. Likely, the OP wants 'a' mode, i.e. "append"

    – juanpa.arrivillaga
    Nov 27 '18 at 17:54








  • 1





    Oh yes, that's true. Anyway all these modes are easily retrieved from the open documentation, i.e. the question is moot, open does create the file if required.

    – Matthieu Brucher
    Nov 27 '18 at 17:55








  • 1





    For reference: docs.python.org/3/library/functions.html#open

    – Matthieu Brucher
    Nov 27 '18 at 17:56

















Have you tried w+ instead of r+?

– Matthieu Brucher
Nov 27 '18 at 17:52







Have you tried w+ instead of r+?

– Matthieu Brucher
Nov 27 '18 at 17:52















You can just next the with inside the try, these are two separate, composable constructs. However, there may be a mode that allows you to not have to use try-except

– juanpa.arrivillaga
Nov 27 '18 at 17:53







You can just next the with inside the try, these are two separate, composable constructs. However, there may be a mode that allows you to not have to use try-except

– juanpa.arrivillaga
Nov 27 '18 at 17:53






2




2





@MatthieuBrucher w+ will truncate if it exists. Likely, the OP wants 'a' mode, i.e. "append"

– juanpa.arrivillaga
Nov 27 '18 at 17:54







@MatthieuBrucher w+ will truncate if it exists. Likely, the OP wants 'a' mode, i.e. "append"

– juanpa.arrivillaga
Nov 27 '18 at 17:54






1




1





Oh yes, that's true. Anyway all these modes are easily retrieved from the open documentation, i.e. the question is moot, open does create the file if required.

– Matthieu Brucher
Nov 27 '18 at 17:55







Oh yes, that's true. Anyway all these modes are easily retrieved from the open documentation, i.e. the question is moot, open does create the file if required.

– Matthieu Brucher
Nov 27 '18 at 17:55






1




1





For reference: docs.python.org/3/library/functions.html#open

– Matthieu Brucher
Nov 27 '18 at 17:56





For reference: docs.python.org/3/library/functions.html#open

– Matthieu Brucher
Nov 27 '18 at 17:56












2 Answers
2






active

oldest

votes


















0














You can open the file in a+ mode (append mode with the permission to read) instead, and do a file seek to position 0 so that it creates the file if it doesn't already exist, but also allows reading the file from the beginning:



with open('ReservationsManagerApp/logs/'+data['booking']+'.json', 'a+') as outfile:
outfile.seek(0)





share|improve this answer































    0














    The mode r+ is read+write, but does not create the file if needed. I think you want w+. Nice table here.






    share|improve this answer
























    • Already said in the comment.

      – Matthieu Brucher
      Nov 27 '18 at 17:57











    • @MatthieuBrucher: Putting answers in comments defeats the purpose of StackOverflow. However, happy to remove my answer if you move your answer to the answer section.

      – Larry Lustig
      Nov 27 '18 at 18:01











    • Well, isn't your link basically saying this is a duplicate? So no need for any answer here.

      – Matthieu Brucher
      Nov 27 '18 at 18:02











    • No. While related, that question does not ask anything about the "open or create" functionality this user is looking for. One of the answers does contain a useful table, however.

      – Larry Lustig
      Nov 27 '18 at 18:05











    • That's your opinion. For me, and for lots of people, this is a duplicate, as OP wants to create a file. The fact that it's in a with clause doesn't change the original problem.

      – Matthieu Brucher
      Nov 27 '18 at 18:07


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can open the file in a+ mode (append mode with the permission to read) instead, and do a file seek to position 0 so that it creates the file if it doesn't already exist, but also allows reading the file from the beginning:



    with open('ReservationsManagerApp/logs/'+data['booking']+'.json', 'a+') as outfile:
    outfile.seek(0)





    share|improve this answer




























      0














      You can open the file in a+ mode (append mode with the permission to read) instead, and do a file seek to position 0 so that it creates the file if it doesn't already exist, but also allows reading the file from the beginning:



      with open('ReservationsManagerApp/logs/'+data['booking']+'.json', 'a+') as outfile:
      outfile.seek(0)





      share|improve this answer


























        0












        0








        0







        You can open the file in a+ mode (append mode with the permission to read) instead, and do a file seek to position 0 so that it creates the file if it doesn't already exist, but also allows reading the file from the beginning:



        with open('ReservationsManagerApp/logs/'+data['booking']+'.json', 'a+') as outfile:
        outfile.seek(0)





        share|improve this answer













        You can open the file in a+ mode (append mode with the permission to read) instead, and do a file seek to position 0 so that it creates the file if it doesn't already exist, but also allows reading the file from the beginning:



        with open('ReservationsManagerApp/logs/'+data['booking']+'.json', 'a+') as outfile:
        outfile.seek(0)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 17:57









        blhsingblhsing

        37.4k41641




        37.4k41641

























            0














            The mode r+ is read+write, but does not create the file if needed. I think you want w+. Nice table here.






            share|improve this answer
























            • Already said in the comment.

              – Matthieu Brucher
              Nov 27 '18 at 17:57











            • @MatthieuBrucher: Putting answers in comments defeats the purpose of StackOverflow. However, happy to remove my answer if you move your answer to the answer section.

              – Larry Lustig
              Nov 27 '18 at 18:01











            • Well, isn't your link basically saying this is a duplicate? So no need for any answer here.

              – Matthieu Brucher
              Nov 27 '18 at 18:02











            • No. While related, that question does not ask anything about the "open or create" functionality this user is looking for. One of the answers does contain a useful table, however.

              – Larry Lustig
              Nov 27 '18 at 18:05











            • That's your opinion. For me, and for lots of people, this is a duplicate, as OP wants to create a file. The fact that it's in a with clause doesn't change the original problem.

              – Matthieu Brucher
              Nov 27 '18 at 18:07
















            0














            The mode r+ is read+write, but does not create the file if needed. I think you want w+. Nice table here.






            share|improve this answer
























            • Already said in the comment.

              – Matthieu Brucher
              Nov 27 '18 at 17:57











            • @MatthieuBrucher: Putting answers in comments defeats the purpose of StackOverflow. However, happy to remove my answer if you move your answer to the answer section.

              – Larry Lustig
              Nov 27 '18 at 18:01











            • Well, isn't your link basically saying this is a duplicate? So no need for any answer here.

              – Matthieu Brucher
              Nov 27 '18 at 18:02











            • No. While related, that question does not ask anything about the "open or create" functionality this user is looking for. One of the answers does contain a useful table, however.

              – Larry Lustig
              Nov 27 '18 at 18:05











            • That's your opinion. For me, and for lots of people, this is a duplicate, as OP wants to create a file. The fact that it's in a with clause doesn't change the original problem.

              – Matthieu Brucher
              Nov 27 '18 at 18:07














            0












            0








            0







            The mode r+ is read+write, but does not create the file if needed. I think you want w+. Nice table here.






            share|improve this answer













            The mode r+ is read+write, but does not create the file if needed. I think you want w+. Nice table here.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 27 '18 at 17:56









            Larry LustigLarry Lustig

            40.3k1284130




            40.3k1284130













            • Already said in the comment.

              – Matthieu Brucher
              Nov 27 '18 at 17:57











            • @MatthieuBrucher: Putting answers in comments defeats the purpose of StackOverflow. However, happy to remove my answer if you move your answer to the answer section.

              – Larry Lustig
              Nov 27 '18 at 18:01











            • Well, isn't your link basically saying this is a duplicate? So no need for any answer here.

              – Matthieu Brucher
              Nov 27 '18 at 18:02











            • No. While related, that question does not ask anything about the "open or create" functionality this user is looking for. One of the answers does contain a useful table, however.

              – Larry Lustig
              Nov 27 '18 at 18:05











            • That's your opinion. For me, and for lots of people, this is a duplicate, as OP wants to create a file. The fact that it's in a with clause doesn't change the original problem.

              – Matthieu Brucher
              Nov 27 '18 at 18:07



















            • Already said in the comment.

              – Matthieu Brucher
              Nov 27 '18 at 17:57











            • @MatthieuBrucher: Putting answers in comments defeats the purpose of StackOverflow. However, happy to remove my answer if you move your answer to the answer section.

              – Larry Lustig
              Nov 27 '18 at 18:01











            • Well, isn't your link basically saying this is a duplicate? So no need for any answer here.

              – Matthieu Brucher
              Nov 27 '18 at 18:02











            • No. While related, that question does not ask anything about the "open or create" functionality this user is looking for. One of the answers does contain a useful table, however.

              – Larry Lustig
              Nov 27 '18 at 18:05











            • That's your opinion. For me, and for lots of people, this is a duplicate, as OP wants to create a file. The fact that it's in a with clause doesn't change the original problem.

              – Matthieu Brucher
              Nov 27 '18 at 18:07

















            Already said in the comment.

            – Matthieu Brucher
            Nov 27 '18 at 17:57





            Already said in the comment.

            – Matthieu Brucher
            Nov 27 '18 at 17:57













            @MatthieuBrucher: Putting answers in comments defeats the purpose of StackOverflow. However, happy to remove my answer if you move your answer to the answer section.

            – Larry Lustig
            Nov 27 '18 at 18:01





            @MatthieuBrucher: Putting answers in comments defeats the purpose of StackOverflow. However, happy to remove my answer if you move your answer to the answer section.

            – Larry Lustig
            Nov 27 '18 at 18:01













            Well, isn't your link basically saying this is a duplicate? So no need for any answer here.

            – Matthieu Brucher
            Nov 27 '18 at 18:02





            Well, isn't your link basically saying this is a duplicate? So no need for any answer here.

            – Matthieu Brucher
            Nov 27 '18 at 18:02













            No. While related, that question does not ask anything about the "open or create" functionality this user is looking for. One of the answers does contain a useful table, however.

            – Larry Lustig
            Nov 27 '18 at 18:05





            No. While related, that question does not ask anything about the "open or create" functionality this user is looking for. One of the answers does contain a useful table, however.

            – Larry Lustig
            Nov 27 '18 at 18:05













            That's your opinion. For me, and for lots of people, this is a duplicate, as OP wants to create a file. The fact that it's in a with clause doesn't change the original problem.

            – Matthieu Brucher
            Nov 27 '18 at 18:07





            That's your opinion. For me, and for lots of people, this is a duplicate, as OP wants to create a file. The fact that it's in a with clause doesn't change the original problem.

            – Matthieu Brucher
            Nov 27 '18 at 18:07



            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks