Home | Lesson 1 | Lesson 2 | Lesson 3 | Lesson 5 | Lesson H | ||
Lesson 4 - Opening Files and a Random Name Generator In this lesson we'll consolidate some of our learning in a novel function. The function will be a random name generator. It will require us to understand how to open, close, read to and write from files. -----------------------------------------------------------------------------------------------------Opening files in read mode To open a text file we would use the following syntax:
we use the open() function with 2 arguments. The first argument is a string of the full path of the file it is important to either use forward slashes / or to use 2 backslashes \\. This is because the backslash in a python string is a special character that encodes things like line breaks and tab breaks The second argument explains how to treat this file, you will either use 'r' to read, 'w' to write, or 'rb' to read binary (forget the last one for now) we should close the file with: In this example the 2 input arguments don't do anything, but if we declare the function with 2 arguements, we have to call it with 2 arguements or python will throw an error. So let's do an example, download the attached "test.py" and place in a folder called "pythonClass" on your C drive (or replace the adress in the example with the right adress)::
You should see a message contained within the text file -----------------------------------------------------------------------------------------------------Opening files in write mode Let's write all the numbers from 0 to 50 into a text file called numbers.txt we will end up using the line: We must write a string to the textfile. We cannot write any other type of variable. We, of course, could pass it a variable as long as that variable is a string. So this means we should create a string of all numbers from 0 to 50, perhaps it would be a good idea to put some kind of marker between them so that they dont all just run into each other like 0123456789101112 and become hard to seperate. we want something that looks like '0,1,2,3,4,5,6,7....' If you think you can do this already then maybe stop reading and give it a go. Otherwise...
-First we declare an empty string called outputString. -Then we say: for the array created by range(51), which is all numbers between 0 and 50 inclusive: -add '%s,'%i to the output string, which means add the string of the number and a comma to the output string -finally get rid of the last comma by setting outputString equal to itself but without the last element If you run this code, you should see a text file created at the appropriate destination. Magic! -----------------------------------------------------------------------------------------------------Let's go the other way and dissect the string contained in the file we just created. We will turn it back into an array of all integers from 0 to 50. Start by opening, reading and closing the file:
So now we have the string: '0,1,2,3,4...' stored in textFileInput. There is a function that operates on strings called split that will come in very handy for what we need to do next. It takes one argument, the character that you want to split the string by, and returns an array of the elements that have been split. Example:
We should see the result: ['', ' th', ' a m', 'understood string?'] Let's use it on our numbers string:
We should see the result: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50'] If we wanted to print a random choice within this array, we could use the random module and the function choice: import random I'm getting a '7' but you could get something else -----------------------------------------------------------------------------------------------------For the tasks, I have created 3 text files, which are downloadable below, they are: 'surname.txt', 'boys.txt' and 'girls.txt' These contain comma separated strings of the most popular surnames, male forenames and female forenames respectively. If you are interested you can see how they were created, you can find the 'nameCreation.py' file in the same directory -----------------------------------------------------------------------------------------------------LESSON 4 TASKS:
test.txtsurname.txtboys.txtgirls.txtlesson4_tasks.py |