Python: A Simple Text File Generator For Beginners

In this tutorial we create a simple Application using Python. Python lets you work more quickly than other programming languages.It is very easy to learn syntax emphasizes readability and reduces time consuming in developing. So let's now do the coding. Setting up the Python First you will need to download & install the Python IDLE's, here the link for the installer for python https://www.python.org/downloads/ After you installed the Pythons IDLE's, run the IDLE and then click file and then new file. After that a new window will appear containing a blank file this will be the text editor for the python. Getting Started To create the program you need to import first a module called os. The os module can be used to perform tasks such as finding the name of present working directory. To do that just kindly copy/paste the code into your text editor.
  1. import os
Creating The Main Function This is the main code to make the simple text generator worked. To do that just kindly copy/paste the code into your text editor and save it as "index.py"
  1. def main():
  2.     print("\n-------------------------------------------------")
  3.     print("!*****|Simple Text File Generator|*****!")
  4.    
  5.     startMenu = ("\nPress 1 to create text file\n" +
  6.         "Press 2 to write the text file\n" +
  7.           "Press 3 to open the text file")
  8.  
  9.     print(startMenu)
  10.  
  11.     txtInput = input("\nChoose a number above and hit enter:")
  12.  
  13.     #Creating a text file    
  14.     if txtInput == '1':
  15.         txtName = input("\nEnter the text file name or type /N to back to menu:")
  16.         if(txtName == "/N"):
  17.            main()
  18.         else:
  19.             while (txtName == ""):
  20.                 print("Please don't leave a blank name")
  21.                 txtName = input("Enter the text file name:")
  22.        
  23.             if(txtName != ""):
  24.                 while(os.path.isfile(txtName + '.txt')):
  25.                     print("\nFile name already exist")
  26.                     txtName = input("Enter the text file name:")
  27.                
  28.             txtFile = open( txtName + ".txt", "w")
  29.             print("\nThe text file has been created!")
  30.             txtFile.close()
  31.             input("\nPress enter to go back to menu..")
  32.             main()
  33.  
  34.     #Writing a text file
  35.     elif txtInput == '2':
  36.         txtName = input("\nEnter the text file name or type /N to back to menu:")
  37.         if(txtName == "/N"):
  38.            main()
  39.         else:
  40.             while not os.path.isfile(txtName):
  41.                 print("\nFile not exist")
  42.                 txtName = input("\nEnter the text file name or type /N to back to menu:")
  43.  
  44.                 if(txtName == "/N"):
  45.                    main()
  46.  
  47.             print("\n" +txtName + " Has been opened!")
  48.             txtMsg = input("Please write something to your text file:")
  49.  
  50.             while (txtMsg == ""):
  51.                 txtMsg = input("Please write something to your text file:")
  52.  
  53.             txtFile = open(txtName, "w")
  54.             txtFile.writelines(txtMsg)
  55.             txtFile.close()
  56.             print("\nSuccesfully write a text!")
  57.             input("Press enter to go back to menu...")
  58.             main()
  59.        
  60.     #Opening a Text File
  61.     elif txtInput == '3':
  62.         txtName = input("\nEnter the text file name or type /N to back to menu:")
  63.  
  64.         if(txtName == "/N"):
  65.            main()
  66.         else:
  67.             while not os.path.isfile(txtName):
  68.                 print("\nFile not exist")
  69.                 txtName = input("\nEnter the text file name or type /N to back to menu:")
  70.  
  71.                 if(txtName == "/N"):
  72.                    main()
  73.                    
  74.             print("Opening the file..\n")
  75.             os.startfile(txtName)
  76.             input("Press enter to go back to menu...")
  77.             main()      
  78.  
  79.     else:
  80.         print("\nPlease enter only the number in the menu\n")
  81.         input("Press enter to continue...")
  82.         main()
  83.  
  84. main()
After the code is completed, try to run the program to see if it works. There you have it we created a simple text file generator in Python. I hope that this basic tutorial helps you understand about Python. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Tags

Add new comment