diff --git a/Examples/FileIO/README.md b/Examples/FileIO/README.md new file mode 100644 index 0000000..df06449 --- /dev/null +++ b/Examples/FileIO/README.md @@ -0,0 +1,4 @@ +# File I/O Examples + +This directory will contain some examples of using Python for file I/O as +Spencer requested. diff --git a/Examples/FileIO/people.dat b/Examples/FileIO/people.dat new file mode 100644 index 0000000..fc8c66c --- /dev/null +++ b/Examples/FileIO/people.dat @@ -0,0 +1,12 @@ +Spencer +Freena +Aaron +Toby +Thomas +Mulbah +Jeff +Shallon +Janet +Daniel +Jallah +Annie diff --git a/Examples/FileIO/sort_people.py b/Examples/FileIO/sort_people.py new file mode 100644 index 0000000..bce937c --- /dev/null +++ b/Examples/FileIO/sort_people.py @@ -0,0 +1,13 @@ +# Load the people from the data file for reading +f = open('people.dat', 'r') +# read in names, one per line +names = f.readlines() +# we're done with the file, close it +f.close() +# each name still has a newline, '\n' on the end of it, let's remove that +cleaned_names = [] +for name in names: + cleaned_names.append(name[:-1]) +# now let's print out the names in alphabetical order +for name in sorted(cleaned_names): + print(name)