From e13412be98415d2f61df922d776b8d2b77f85532 Mon Sep 17 00:00:00 2001 From: Sylvain PILLOT Date: Thu, 15 Feb 2024 23:19:45 +0100 Subject: [PATCH] added 6 working example used for testing file IO --- ports/sh/examples/file/launch_me_first.py | 3 +++ ports/sh/examples/file/read1.py | 4 ++++ ports/sh/examples/file/read2.py | 4 ++++ ports/sh/examples/file/test_fileopenread.py | 9 +++++++++ ports/sh/examples/file/write1.py | 9 +++++++++ ports/sh/examples/file/write2.py | 9 +++++++++ 6 files changed, 38 insertions(+) create mode 100644 ports/sh/examples/file/launch_me_first.py create mode 100644 ports/sh/examples/file/read1.py create mode 100644 ports/sh/examples/file/read2.py create mode 100644 ports/sh/examples/file/test_fileopenread.py create mode 100644 ports/sh/examples/file/write1.py create mode 100644 ports/sh/examples/file/write2.py diff --git a/ports/sh/examples/file/launch_me_first.py b/ports/sh/examples/file/launch_me_first.py new file mode 100644 index 000000000..47daece96 --- /dev/null +++ b/ports/sh/examples/file/launch_me_first.py @@ -0,0 +1,3 @@ +f = open("demofile.txt", "w") +f.write("Hello! Welcome to demofile.txt\nThis file is for testing purposes.\nGood Luck!\n") +f.close() diff --git a/ports/sh/examples/file/read1.py b/ports/sh/examples/file/read1.py new file mode 100644 index 000000000..96cb1dd85 --- /dev/null +++ b/ports/sh/examples/file/read1.py @@ -0,0 +1,4 @@ +f = open("demofile.txt", "r") +print(f.read()) +f.close() + diff --git a/ports/sh/examples/file/read2.py b/ports/sh/examples/file/read2.py new file mode 100644 index 000000000..0fd82f8e3 --- /dev/null +++ b/ports/sh/examples/file/read2.py @@ -0,0 +1,4 @@ +f = open("demofile.txt", "r") +print(f.read(5)) +f.close() + diff --git a/ports/sh/examples/file/test_fileopenread.py b/ports/sh/examples/file/test_fileopenread.py new file mode 100644 index 000000000..2b6181866 --- /dev/null +++ b/ports/sh/examples/file/test_fileopenread.py @@ -0,0 +1,9 @@ +f = open("demofile2.txt", "a") +f.write("Now the file has more content!") +f.close() + +#open and read the file after the appending: +f = open("demofile2.txt", "r") +print(f.read()) +f.close() + diff --git a/ports/sh/examples/file/write1.py b/ports/sh/examples/file/write1.py new file mode 100644 index 000000000..2b6181866 --- /dev/null +++ b/ports/sh/examples/file/write1.py @@ -0,0 +1,9 @@ +f = open("demofile2.txt", "a") +f.write("Now the file has more content!") +f.close() + +#open and read the file after the appending: +f = open("demofile2.txt", "r") +print(f.read()) +f.close() + diff --git a/ports/sh/examples/file/write2.py b/ports/sh/examples/file/write2.py new file mode 100644 index 000000000..64f50ed64 --- /dev/null +++ b/ports/sh/examples/file/write2.py @@ -0,0 +1,9 @@ +f = open("demofile3.txt", "w") +f.write("Woops! I have deleted the content!") +f.close() + +#open and read the file after the overwriting: +f = open("demofile3.txt", "r") +print(f.read()) +f.close() +