summaryrefslogtreecommitdiffhomepage
path: root/python.html.markdown
diff options
context:
space:
mode:
authorBoris Verkhovskiy <boris.verk@gmail.com>2024-04-03 04:31:27 -0700
committerGitHub <noreply@github.com>2024-04-03 04:31:27 -0700
commitc45730e144f739eee683c0993b6fb40acce4b77a (patch)
tree56da43c86e1aebd24e3913b405e21d6f2812e9a3 /python.html.markdown
parentc166f2acb295627c5ae305a6dd517a27ca8fece6 (diff)
parentfbf132752b743d0f43c3395da0699bee53da22df (diff)
Merge pull request #4409 from TanayParikh/patch-1
[python/en] Python Update `open` File Open `mode`
Diffstat (limited to 'python.html.markdown')
-rw-r--r--python.html.markdown8
1 files changed, 4 insertions, 4 deletions
diff --git a/python.html.markdown b/python.html.markdown
index d863bcc3..9d97e64d 100644
--- a/python.html.markdown
+++ b/python.html.markdown
@@ -501,20 +501,20 @@ with open("myfile.txt") as f:
# Writing to a file
contents = {"aa": 12, "bb": 21}
-with open("myfile1.txt", "w+") as file:
+with open("myfile1.txt", "w") as file:
file.write(str(contents)) # writes a string to a file
import json
-with open("myfile2.txt", "w+") as file:
+with open("myfile2.txt", "w") as file:
file.write(json.dumps(contents)) # writes an object to a file
# Reading from a file
-with open('myfile1.txt', "r+") as file:
+with open('myfile1.txt', "r") as file:
contents = file.read() # reads a string from a file
print(contents)
# print: {"aa": 12, "bb": 21}
-with open('myfile2.txt', "r+") as file:
+with open('myfile2.txt', "r") as file:
contents = json.load(file) # reads a json object from a file
print(contents)
# print: {"aa": 12, "bb": 21}