w3resource

Python File I/O: Generate 26 text files named A.txt, B.txt, and so on up to Z.txt


20. Generate 26 Text Files

Write a Python program to generate 26 text files named A.txt, B.txt, and so on up to Z.txt.

Sample Solution:

Python Code:

import string, os
if not os.path.exists("letters"):
   os.makedirs("letters")
for letter in string.ascii_uppercase:
   with open(letter + ".txt", "w") as f:
       f.writelines(letter)

Flowchart:

Flowchart: File I/O: Generate 26 text files named A.txt, B.txt, and so on up to Z.txt.

For more Practice: Solve these Related Problems:

  • Write a Python program to generate 26 text files named A.txt through Z.txt and write the corresponding letter into each file.
  • Write a Python script to create 26 text files (one per alphabet letter) and then insert a random quote into each file.
  • Write a Python program to generate 26 text files named after each letter and append the letter’s ASCII value to each file.
  • Write a Python program to create 26 files for the English alphabet and then verify the existence of each file in the directory.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to extract characters from various text files and puts them into a list.
Next: Write a Python program to create a file where all letters of English alphabet are listed by specified number of letters on each line.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.