Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDinko Galetic <dgaletic@everflame.(none)>2010-06-03 07:36:19 (GMT)
committer Dinko Galetic <dgaletic@everflame.(none)>2010-06-03 07:36:19 (GMT)
commit14e9dd363e755a00b5fc7f611b7cd2698bb5fb4f (patch)
treef12fd61b5ed537a9f083f01cfd498cd4d70d9d86
parent355af3e4685d9f5918f0ae3452b61df74ad04a46 (diff)
Added a Python script which demonstrates how to write to and read a text file.
-rw-r--r--data/GSOC examples/file writing and reading41
1 files changed, 41 insertions, 0 deletions
diff --git a/data/GSOC examples/file writing and reading b/data/GSOC examples/file writing and reading
new file mode 100644
index 0000000..13b7436
--- /dev/null
+++ b/data/GSOC examples/file writing and reading
@@ -0,0 +1,41 @@
+import time
+
+print "This example demonstrates how to create a text file, write something to it and then read what we wrote."
+
+print "How shall we call the file?"
+filename = raw_input()
+
+print "Where would you like to store the file? (can be left empty)"
+# Practice: Where does the file get stored if we leave this empty?
+directory = raw_input()
+
+full_name = directory + filename
+
+print "Please enter the text you would like to store."
+text = raw_input()
+
+# Open the file in the "w"rite mode.
+my_file = open(full_name, "w")
+
+# Let's write the current time to that file.
+# \n is the way of saying "i'm finished with that line, move to the next one."
+current_time = time.ctime()
+my_file.write("The following text was written at: " + current_time + "\n")
+
+# Write our text to the open file.
+my_file.write(text)
+my_file.write("\n")
+
+# Close the file.
+my_file.close()
+
+# Now lets read from that same file. This is how reading is done.
+# First: Open the file in "r"ead mode.
+my_file = open(full_name, "r")
+# Second: Read from it.
+from_file = my_file.read()
+# Third: Close the file.
+my_file.close()
+
+# Print what we've read.
+print from_file