Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
The very basics of text files
7. Real world example
with statement and exception handling


Good job! Because of its simplicity, Python is frequently the language of choice when we need to generate text files with some contents. For instance, say you need a file that will list photo names, from IMG_1.jpg to IMG_100.jpg. Manually, it would take a lot of time. With Python, you only need a few lines of code:

file = open('photos_from_vacation.txt', 'w')
for i in range(1, 101):

See how easy it is? We only needed to open a new file and use a simple for loop to get a hundred file names at once.


A student wants to download past examinations in all the subjects given in the subjects list of the template code. To that end, the student needs links in the following format:{subject}-{year}

For instance, the past paper in math from 2015 would be:

Create a file named urls.txt that will have all necessary links separated with newlines. You need to include links for all subjects and all years from 2010 to 2018 (inclusive). Start with the first subject, and generate its links for all years. Then, move on to the next subject, and so on.

Stuck? Here's a hint!

You will need nested for loops:

for subject in subjects:
  for i in range(2010, 2019):

Remember that a newline character is inserted with '\n'.