Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import string
- # global F_Contents Corrected - was F_Content, moved inside function
- def F_info():
- global space_count
- global F_Contents
- global F_hand
- F_hand = ""
- file = ""
- F_hand = input("\nWhat file would you like to find?\n")
- if not F_hand.endswith(".txt"):
- F_hand += ".txt"
- print("\nAttempting to open:", F_hand, "\n")
- time.sleep(0.4)
- try:
- with open(F_hand, "r") as file:
- F_Contents = file.read()
- print(F_Contents)
- except:
- print("\nfile does not exist\nplease enter a valid file name\n")
- F_info()
- def opchoice():
- print ("------------------------------------------------------------")
- operation = input(
- """\n\nwhich of the following would you like to do?\nA: Count all letters\nB: Count all words\nC: Count how many times a specific letter appears\nD: Count how many times a specific word or phrase appears\nE: View entire file in uppercase letters\nF: View entire file in lowercase letters\nG: Exit the program\n""").upper()
- print ("------------------------------------------------------------")
- if operation == "A":
- with open (F_hand, "r") as Afile:
- data = Afile.read().replace(" ","").replace("+","").replace("×","").replace("÷","").replace("=","").replace("%","").replace("/","").replace("*","").replace("€","").replace("£","").replace("@","").replace("$","").replace("","").replace("","").replace("","").replace('',"").replace("!","").replace("#","").replace(":","").replace(";","").replace("&","").replace("_","").replace("(","").replace(")","").replace("-","").replace("'","").replace('"',"").replace(",","").replace(".","").replace("?","").replace("1","").replace("2","").replace("3","").replace("4","").replace("5","").replace("6","").replace("7","").replace("8","").replace("9","").replace("0","")
- Alen = len(data)
- print (
- "\nThe total number of letters is: ",Alen,"\n"
- )
- #part of a bigger program but this operarion choice
- #should find the number of words in a file
- #without counting any special characters, numbers, or #individual letters
- elif operation == "B":
- #W_num is number of words
- #F_Contents is whats inside the file
- W_num = sum([i.strip(string.punctuation).isalpha() for i in F_Contents.split()])
- print ("The number of words is:",W_num,"\n")
- #Should only output number of words, but for me it is #counting individual letters as words too
- elif operation == "C":
- letter = input("\nEnter a letter\n").lower()
- Al_char = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"]
- while letter not in Al_char:
- letter = input("Choose a single letter or number\n").lower()
- L_count = (F_Contents.lower().count(letter))
- print ("The letter",letter,"appears",L_count,"times")
- elif operation == "D":
- phrase = input("\nEnter a word or phrase\n").lower()
- while (len(phrase)) <= 1:
- phrase = input("\nenter a valid word or phrase or series of letters\n").lower()
- ph_count = F_Contents.lower().count(phrase)
- print(ph_count)
- elif operation == "E":
- print(F_Contents.upper())
- elif operation == "F":
- print(F_Contents.lower())
- elif operation == "G" or "EXIT" or "QUIT" or "DONE":
- exit()
- else:
- print("Please choose a valid operation from the list of operations.(A-G)")
- opchoice()
- def retry():
- choice = input("Would you like to find another file? (y/n)\nIf you would like to use the same file and use a new\n operation instead enter (same)\n").lower()
- if choice == "y":
- F_info()
- opchoice()
- retry()
- elif choice == "same":
- opchoice()
- retry()
- else:
- exit()
- with open("testfile.txt", "w+") as test:
- test.write("The dog walked across the room.")
- with open("testfile.txt", "r") as test:
- contents = test.read()
- print("checking contents of testfile.txt:\n", contents)
- with open("breaktest.txt","w+") as test2:
- test2.write("+ w d ×%+/÷ *€× h &#? hello '1 572 2 ()$_")
- with open("breaktest.txt","r") as test2:
- contents2 = test2.read()
- print ("checking contents of breaktest.txt\n", contents2)
- F_info()
- opchoice()
- retry()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement