Connot write a file : I have the following code:

    import csv
    import operator
    import sys
    
    with open('countryInfo.csv','r', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile, delimiter='\t')
        result = sorted(reader, key = lambda d: float(d['population']),reverse = True)
     #   for row in result:
     #       print(row)
     #   for row in result:
     #      print(row['name'], row['capital'], row['population'])
    writer = csv.DictWriter(open('country_simple_info.csv', 'w', encoding='utf-8'), reader.fieldnames)
    #with open("country_simple_info.csv", "w", encoding='utf-8') as csvoutfile:
    writer.writeheader()
    writer.writerows(result)

The goal of this code is to write a program that opens a countryInfo.csv file and extracts the country name, capital city and population from each row, then writes a new file named country_simple_info.csv with country, capital and population in each row, with the rows sorted by population size, largest first. The file has columns with other information such as continent, languages, etc. but the code should ignore those. In my code above, when I uncomment the print statements, the code can print the expected output- something in the following format:

    country,capital,population 
    China,Beijing,1330044000
    India,New Delhi,1173108018
    United States,Washington,310232863
    .......

However, I cannot get the file to be written. Any ideas? And also, I am not allowed to use pandas. 