Python: A Simple Text File Generator For Beginners
Submitted by razormist on Tuesday, April 4, 2017 - 19:21.
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.
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"
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!!
- import os
- def main():
- print("\n-------------------------------------------------")
- print("!*****|Simple Text File Generator|*****!")
- startMenu = ("\nPress 1 to create text file\n" +
- "Press 2 to write the text file\n" +
- "Press 3 to open the text file")
- print(startMenu)
- txtInput = input("\nChoose a number above and hit enter:")
- #Creating a text file
- if txtInput == '1':
- txtName = input("\nEnter the text file name or type /N to back to menu:")
- if(txtName == "/N"):
- main()
- else:
- while (txtName == ""):
- print("Please don't leave a blank name")
- txtName = input("Enter the text file name:")
- if(txtName != ""):
- while(os.path.isfile(txtName + '.txt')):
- print("\nFile name already exist")
- txtName = input("Enter the text file name:")
- txtFile = open( txtName + ".txt", "w")
- print("\nThe text file has been created!")
- txtFile.close()
- input("\nPress enter to go back to menu..")
- main()
- #Writing a text file
- elif txtInput == '2':
- txtName = input("\nEnter the text file name or type /N to back to menu:")
- if(txtName == "/N"):
- main()
- else:
- while not os.path.isfile(txtName):
- print("\nFile not exist")
- txtName = input("\nEnter the text file name or type /N to back to menu:")
- if(txtName == "/N"):
- main()
- print("\n" +txtName + " Has been opened!")
- txtMsg = input("Please write something to your text file:")
- while (txtMsg == ""):
- txtMsg = input("Please write something to your text file:")
- txtFile = open(txtName, "w")
- txtFile.writelines(txtMsg)
- txtFile.close()
- print("\nSuccesfully write a text!")
- input("Press enter to go back to menu...")
- main()
- #Opening a Text File
- elif txtInput == '3':
- txtName = input("\nEnter the text file name or type /N to back to menu:")
- if(txtName == "/N"):
- main()
- else:
- while not os.path.isfile(txtName):
- print("\nFile not exist")
- txtName = input("\nEnter the text file name or type /N to back to menu:")
- if(txtName == "/N"):
- main()
- print("Opening the file..\n")
- os.startfile(txtName)
- input("Press enter to go back to menu...")
- main()
- else:
- print("\nPlease enter only the number in the menu\n")
- input("Press enter to continue...")
- main()
- main()
Add new comment
- 102 views