From efd0a4590a92d49189751453d4031636b38ffa51 Mon Sep 17 00:00:00 2001 From: Riadh Bch <47902351+rb-x@users.noreply.github.com> Date: Sat, 16 May 2020 00:29:05 +0200 Subject: [PATCH 1/3] Correction of small typing errors There was some minor mistakes in the file manipulation section, so I just rectified them. --- python.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/python.md b/python.md index f885717b8..7c1e1319a 100644 --- a/python.md +++ b/python.md @@ -122,15 +122,17 @@ category: Python file = open("hello.txt", "r") # open in read mode 'r' file.close() -print(file.read()) # read the file -print fh.readline() # Reading line by line +--- +print(file.read()) # read the entire file and set the cursor at the end of file +print file.readline() # Reading one line +file.seek(0, 0) # place the cursor at the beggining of the file ``` ### Writing (overwrite) ```py file = open("hello.txt", "w") # open in write mode 'w' -write("Hello World") +file.write("Hello World") text_lines = ["First line", "Second line", "Last line"] file.writelines(text_lines) @@ -142,7 +144,7 @@ file.close() ```py file = open("Hello.txt", "a") # open in append mode -write("Hello World again") +file.write("Hello World again") file.close() ``` @@ -150,8 +152,9 @@ file.close() ```py with open("welcome.txt", "r") as file: - # 'file' refers directly to the "welcome.txt" + # 'file' refers directly to "welcome.txt" data = file.read() + +# It closes the file automatically at the end of scope, no need for `file.close()`. ``` -It closes the file automatically, no need for `file.close()`. From 574355b794541414155cf31e27dc49105735b5b9 Mon Sep 17 00:00:00 2001 From: Riadh Bch <47902351+rb-x@users.noreply.github.com> Date: Sat, 16 May 2020 00:48:21 +0200 Subject: [PATCH 2/3] f string fix --- python.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python.md b/python.md index 7c1e1319a..852c8a0f4 100644 --- a/python.md +++ b/python.md @@ -58,7 +58,7 @@ category: Python string.replace("-", " ") ",".join(list) "hi {0}".format('j') - "hi {name}" # same as "hi {}".format('name') + f"hi {name}" # same as "hi {}".format('name') str.find(",") str.index(",") # same, but raises IndexError str.count(",") From 0114a74db2166bf239b35cc02ed3d2b434adbc50 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Sat, 13 Jun 2020 10:09:01 +1000 Subject: [PATCH 3/3] Update python.md --- python.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python.md b/python.md index 852c8a0f4..813a88801 100644 --- a/python.md +++ b/python.md @@ -121,8 +121,9 @@ category: Python ```py file = open("hello.txt", "r") # open in read mode 'r' file.close() +``` ---- +```py print(file.read()) # read the entire file and set the cursor at the end of file print file.readline() # Reading one line file.seek(0, 0) # place the cursor at the beggining of the file