From e024cfa0a0e0e9a1d21b32ed347279872793c2af Mon Sep 17 00:00:00 2001 From: Andy <159312225+Mw4mba@users.noreply.github.com> Date: Sat, 28 Sep 2024 09:37:56 +0200 Subject: [PATCH] Create file operations.md Signed-off-by: Andy <159312225+Mw4mba@users.noreply.github.com> --- python/file operations.md | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 python/file operations.md diff --git a/python/file operations.md b/python/file operations.md new file mode 100644 index 0000000..337b27a --- /dev/null +++ b/python/file operations.md @@ -0,0 +1,55 @@ +Python gives a very powerful and versatile user interface that allows you to interact with files very efficiently. +It follows the general syntax: + + f = open(filename, mode) + + +## Operations +There are certain modes you can open a file in, and good rule of thumb on their usage is the type of operation you are trying to complete +### Read +r: This is to open a file in your program +r+:This allows you to read and write to a file, it does not overwrite any pre-existing data in the file +allowing you to primarily read but + + file = open('pyData.txt','r') + print(file.readline(1)) + #This will read the first line in your file, and is one of the methods you can use +You can use plain read to extract the contents of the file: + + file = open('pyData.txt','r') + fileContent=file.read() + print(fileContent) +### Write: +w: This is to primarily write, giving you full writing privileges allowing you to also overwrite any pre-existing data in the program + + file=open('myData.txt','w') + file.write("The quick brown fox jumps over the lazy dog") + +w+: This is extended writing, allowing you to create a file if it does not exist ,you can also read data from a given file with the intent to write to it, it will effectively reset the file allowing you to write to it. + + + file=open('myData.txt','w+') + file.write("The quick brown fox jumps over the lazy dog") + print(file.read()) + +### Append: +a: This is to append, or add to the file without overwriting any pre-existing data + + file=open('myData.txt','a') + file.write("The quick brown fox jumps over the lazy dog again") + +a+: This is to append and read data from the file, without overriding any pre-existing data in the file. + + file=open('myData.txt','a+') + file.write("The quick brown fox runs from the lazy dog") + print(file.read()) +## Create +x+: this creates the file and will return an error if the file already exists +e.g.: + + file=open('myData.txt','x') + file.write("The quick brown fox jumps over the lazy dog") + newfile=open('myData.txt','r') + print(myData.read()) + +