From d5623e46fb618326b21e77e5174a2c35899a386e Mon Sep 17 00:00:00 2001 From: Riadh Bch <47902351+rb-x@users.noreply.github.com> Date: Sun, 24 Nov 2019 18:19:00 +0100 Subject: [PATCH 1/2] Update python.md Adding file managing cheatsheet --- python.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/python.md b/python.md index 26b8c7760..922ef4bae 100644 --- a/python.md +++ b/python.md @@ -58,6 +58,7 @@ category: Python string.replace("-", " ") ",".join(list) "hi {0}".format('j') + "hi {name}" # same as "hi {}".format('name') str.find(",") str.index(",") # same, but raises IndexError str.count(",") @@ -101,3 +102,35 @@ category: Python expr.match(...) expr.sub(...) +### File manipulation + + # Reading + + file = open("hello.txt", "r") #open the file in readmode 'r' + file.close() + + print(file.read()) # read the file + print fh.readline() # Reading line by line + + # Writing ( Overwrite the previous content ! ) + + file = open("hello.txt","w") # open the fil in write mode + write("Hello World") + + text_lines = ["First line", "Second line", "a third and last line"] + file.writelines(text_lines) # write the 3 line above inside hello.txt + + file.close() + + # Append File + + file = open("Hello.txt", "a") #open file in append mode + write("Hello World again") + file.close() + + # Context Manager + + with open("welcome.txt" ,"r") as file: # file refer directly to the "welcome.txt" , mode = r , w , a + data = file.read() + # it close the file automatically no need for file.close() + From 947d9c313536771666026a7d9f36f60b48e99096 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Wed, 18 Dec 2019 13:41:29 +1100 Subject: [PATCH 2/2] Update formatting --- python.md | 71 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/python.md b/python.md index 922ef4bae..0ef2df680 100644 --- a/python.md +++ b/python.md @@ -102,35 +102,44 @@ category: Python expr.match(...) expr.sub(...) -### File manipulation - - # Reading - - file = open("hello.txt", "r") #open the file in readmode 'r' - file.close() - - print(file.read()) # read the file - print fh.readline() # Reading line by line - - # Writing ( Overwrite the previous content ! ) - - file = open("hello.txt","w") # open the fil in write mode - write("Hello World") - - text_lines = ["First line", "Second line", "a third and last line"] - file.writelines(text_lines) # write the 3 line above inside hello.txt - - file.close() - - # Append File - - file = open("Hello.txt", "a") #open file in append mode - write("Hello World again") - file.close() - - # Context Manager - - with open("welcome.txt" ,"r") as file: # file refer directly to the "welcome.txt" , mode = r , w , a - data = file.read() - # it close the file automatically no need for file.close() +## File manipulation +### Reading + +```py +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 +``` + +### Writing (overwrite) + +```py +file = open("hello.txt", "w") # open in write mode 'w' +write("Hello World") + +text_lines = ["First line", "Second line", "Last line"] +file.writelines(text_lines) + +file.close() +``` + +### Writing (append) + +```py +file = open("Hello.txt", "a") # open in append mode +write("Hello World again") +file.close() +``` + +### Context manager + +```py +with open("welcome.txt", "r") as file: + # 'file' refers directly to the "welcome.txt" + data = file.read() +``` + +It closes the file automatically, no need for `file.close()`.