Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the code in PYTHON 3. The file WorldSeriesWinners.txt contains a chronolog

ID: 3866330 • Letter: W

Question

Write the code in PYTHON 3.

The file WorldSeriesWinners.txt contains a chronological list of the World Series winning teams from 1903 through 2009. (The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2009. Note that the World Series was not played in 1904 or 1994.) Write a program that reads the contents of the WorldSeriesWinners.txt file into a list. When the user enters the name of a team, the program displays the number of times that team has won the World Series in the time period from 1903 through 2009. You must create and use at least 2 meaningful functions (your choice). 2. Prompt for the input file. Your program must check if the file exists. If it does not, your program should output a "file not found" message and keep asking until a correct file is entered. 3. Name the source code file "Teams.py" Sample run: Here is a sample run of the program: Enter the name of a team: New York Yankees The New York Yankees won the world series 26 times between 1903 and 2009.

Explanation / Answer

checkout below link for your referacne

http://interactivepython.org/runestone/static/pip/Files/files.html

Working with Data Files

So far, the data we have used in this book have all been either coded right into the program, or have been entered by the user. In real life data reside in files. For example the images we worked with in the image processing unit ultimately live in files on your hard drive. Web pages, and word processing documents, and music are other examples of data that live in files. In this short chapter we will introduce the Python concepts necessary to use data from files in our programs.

For our purposes, we will assume that our data files are text files–that is, files filled with characters. The Python programs that you write are stored as text files. We can create these files in any of a number of ways. For example, we could use a text editor to type in and save the data. We could also download the data from a website and then save it in a file. Regardless of how the file is created, Python will allow us to manipulate the contents.

In Python, we must open files before we can use them and close them when we are done with them. As you might expect, once a file is opened it becomes a Python object just like all other data. Table 1 shows the functions and methods that can be used to open and close files.

Finding a File on your Disk

Opening a file requires that you, as a programmer, and Python agree about the location of the file on your disk. The way that files are located on disk is by their path You can think of the filename as the short name for a file, and the path as the full name. For example on a Mac if you save the file hello.txt in your home directory the path to that file is /Users/yourname/hello.txt On a Windows machine the path looks a bit different but the same principles are in use. For example on windows the path might be C:UsersyournameMy Documentshello.txt

You can access files in folders, also called directories, under your home directory by adding a slash and the name of the folder. For example, if you had a file called hello.py in a folder called SI106 that was inside a folder called PyCharmProjects under your home directory, then the full name for hello.py stored in the CS150 folder would be /Users/yourname/PyCharmProjects/SI106/hello.py

Here’s the important rule to remember: If your file and your Python program are in the same directory you can simply use the filename. open('myfile.txt','r') If your file and your Python program are in different directories then you should use the path to the file open(/Users/joebob01/myfile.txt).

Reading a File

As an example, suppose we have a text file called qbdata.txt that contains the following data representing statistics about NFL quarterbacks. Although it would be possible to consider entering this data by hand each time it is used, you can imagine that it would be time-consuming and error-prone to do this. In addition, it is likely that there could be data from more quarterbacks and other years. The format of the data file is as follows

To open this file, we would call the open function. The variable, fileref, now holds a reference to the file object returned by open. When we are finished with the file, we can close it by using the closemethod. After the file is closed any further attempts to use fileref will result in an error.

Iterating over lines in a file

We will now use this file as input in a program that will do some data processing. In the program, we will read each line of the file and print it with some additional text. Because text files are sequences of lines of text, we can use the for loop to iterate through each line of the file.

A line of a file is defined to be a sequence of characters up to and including a special character called the newline character. If you evaluate a string that contains a newline character you will see the character represented as . If you print a string that contains a newline you will not see the , you will just see its effects. When you are typing a Python program and you press the enter or return key on your keyboard, the editor inserts a newline character into your text at that point.

As the for loop iterates through each line of the file the loop variable will contain the current line of the file as a string of characters. The general pattern for processing each line of a text file is as follows:

To process all of our quarterback data, we will use a for loop to iterate over the lines of the file. Using the split method, we can break each line into a list containing all the fields of interest about the quarterback. We can then take the values corresponding to first name, lastname, and passer rating to construct a simple sentence.

Alternative File Reading Methods

In addition to the for loop, Python provides three other methods to read data from the input file. You don’t really need to use all of them, but in case you encounter them, here’s what they do. The readline method reads one line from the file and returns it as a string. The string returned by readline will contain the newline character at the end. This method returns the empty string when it reaches the end of the file. The readlines method returns the contents of the entire file as a list of strings, where each item in the list represents one line of the file. It is also possible to read the entire file into a single string with read. Table 2 summarizes these methods and the following session shows them in action.

In this course, we will generally either iterate through the contents of a file using a for loop, or use read() to get all of the contents as a single string.

In other programming languages, where they don’t have the convenient for loop method of going through the lines of the file one by one, they use a different pattern which requires a different kind of loop, the while loop. Fortunately, you don’t need to learn this other pattern, and we will put off consideration of while loops indefinitely in this course.

Note

A common error that novice programmers make is not realizing that all these ways of reading the file, including using the for loop to iterate over the lines, use up the file. If you want to go through the contents twice, for now it is probably easiest to just open it twice, and iterate through it twice. If you cared about efficiency, you might prefer to save the contents in a variable.

Writing Text Files

One of the most commonly performed data processing tasks is to read data from a file, manipulate it in some way, and then write the resulting data out to a new data file to be used for other purposes later. To accomplish this, the open function discussed above can also be used to create a new file prepared for writing. Note in Table 1 above that the only difference between opening a file for writing and opening a file for reading is the use of the 'w' flag instead of the 'r' flag as the second parameter. When we open a file for writing, a new, empty file with that name is created and made ready to accept our data. As before, the function returns a reference to the new file object.

Table 2 above shows one additional file method that we have not used thus far. The write method allows us to add data to a text file. Recall that text files contain sequences of characters. We usually think of these character sequences as being the lines of the file where each line ends with the newline character. Be very careful to notice that the write method takes one parameter, a string. When invoked, the characters of the string will be added to the end of the file. This means that it is the programmer’s job to include the newline characters as part of the string if desired.

As an example, consider the qbdata.txt file once again. Assume that we have been asked to provide a file consisting of only the names of the quarterbacks. In addition, the names should be in the order last name followed by first name with the names separated by a comma. This is a very common type of request, usually due to the fact that someone has a program that requires its data input format to be different from what is available.

To construct this file, we will approach the problem using a similar algorithm as above. After opening the file, we will iterate thru the lines, break each line into its parts, choose the parts that we need, and then output them. Eventually, the output will be written to a file.

The program below solves part of the problem. Notice that it reads the data and creates a string consisting of last name followed by a comma followed by the first name. In this example, we simply print the lines as they are created.

Method Name Use Explanation open open(filename,'r') Open a file called filename and use it for reading. This will return a reference to a file object. open open(filename,'w') Open a file called filename and use it for writing. This will also return a reference to a file object. close filevariable.close() File use is complete.