write a python program to generate 26 text files named a.txt b.txt and so on up to z.txt

In this article let's try to write a Python program to create 26 text files named in alphabetical order from a to z

 

 

write a python program to generate 26 text files named a.txt, b.txt, and so on up to z.txt

 



string.ascii_letters will provide the the list of alphabets from small a to z

Generate the set of alphabets from a to z, then using the for loop Use open() function and pass the the alphabet.txt ext extension as the file name, and open the file in write mode this will create a new  text file.  

Immediately close the the text file using the file descriptor and close function call

 

Write a python program to generate 26 text files named a.txt, b.txt, and so on up to z.txt

import string

for letter in string.ascii_letters:
    fd = open(letter + ".txt", "w")
    fd.close()

 


Write a python program to generate 26 text files named a.txt, b.txt, and so on up to z.txt using while loop


Also  you can do this using a while loop to generate the list of characters from a to z. Just take the letter of a and add and till it becomes z using a while loop.

While you  iterate from a to z,  use the open function  to create a file in the right more and immediately close it


start = ord("a")
while chr(start) != "z":
   fd = open(chr(start) + ".txt", "w")
   fd.close()
   start += 1



Conclusion: Try to run the program by yourself and comment on below if you have any queries or suggestions.

Post a Comment

0 Comments