Python open or create file using with clause [duplicate]
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?
python
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.
|
show 1 more comment
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?
python
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 thewith
inside thetry
, these are two separate, composable constructs. However, there may be a mode that allows you to not have to usetry-except
– juanpa.arrivillaga
Nov 27 '18 at 17:53
2
@MatthieuBrucherw+
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
|
show 1 more comment
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?
python
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
python
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 thewith
inside thetry
, these are two separate, composable constructs. However, there may be a mode that allows you to not have to usetry-except
– juanpa.arrivillaga
Nov 27 '18 at 17:53
2
@MatthieuBrucherw+
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
|
show 1 more comment
Have you tried w+ instead of r+?
– Matthieu Brucher
Nov 27 '18 at 17:52
You can just next thewith
inside thetry
, these are two separate, composable constructs. However, there may be a mode that allows you to not have to usetry-except
– juanpa.arrivillaga
Nov 27 '18 at 17:53
2
@MatthieuBrucherw+
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
|
show 1 more comment
2 Answers
2
active
oldest
votes
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)
add a comment |
The mode r+
is read+write, but does not create the file if needed. I think you want w+
. Nice table here.
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 awith
clause doesn't change the original problem.
– Matthieu Brucher
Nov 27 '18 at 18:07
|
show 3 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 27 '18 at 17:57
blhsingblhsing
37.4k41641
37.4k41641
add a comment |
add a comment |
The mode r+
is read+write, but does not create the file if needed. I think you want w+
. Nice table here.
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 awith
clause doesn't change the original problem.
– Matthieu Brucher
Nov 27 '18 at 18:07
|
show 3 more comments
The mode r+
is read+write, but does not create the file if needed. I think you want w+
. Nice table here.
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 awith
clause doesn't change the original problem.
– Matthieu Brucher
Nov 27 '18 at 18:07
|
show 3 more comments
The mode r+
is read+write, but does not create the file if needed. I think you want w+
. Nice table here.
The mode r+
is read+write, but does not create the file if needed. I think you want w+
. Nice table here.
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 awith
clause doesn't change the original problem.
– Matthieu Brucher
Nov 27 '18 at 18:07
|
show 3 more comments
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 awith
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
|
show 3 more comments
Have you tried w+ instead of r+?
– Matthieu Brucher
Nov 27 '18 at 17:52
You can just next the
with
inside thetry
, these are two separate, composable constructs. However, there may be a mode that allows you to not have to usetry-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