0

I am trying to create an anagram program quiz. One of the things I have done is having one central method of reading from a specified file dependant on which option the user has chosen rather than having to repeat the code. However, when trying to save the info to file, the variable saved has the pathfile saved inside it. How can I split it so that it will only save the name of the file (i.e, name of the quiz) that has been opened?

def main():
    name = input("Please Enter your name to begin")
    print("Hi",name,"Welcome to the Computer Science Quiz")
    user_choice = menu()
    option = choice(user_choice)
    option, counter = questions(option)
    update_file(name, option, counter)

def menu():
    print("Select from the following categories:")
    print("1 for System's Architecture, 2 for Memory, 3 for Storage or 4 for Network Topologies")
    choice = int(input("choose option"))
    if choice >0 and choice<=4: ## range check
        print("you have chosen",choice,)
    else:
        print("This is an invalid number")
        menu()      
    return choice

def choice(user_choice):
    if user_choice == 1:
        systems = open('systems.csv','r')
        return systems
    elif user_choice ==2:
        memory = open('memory.csv','r')
        return memory
    else:
        storage = open('storage.csv','r')
        return storage

def questions(option):
    counter = 0
    for line in option:
        anagrams = (line.rstrip("\n").split(","))
        question = anagrams[0]
        answer = anagrams[1]
        print (question)
        print (answer)
        guess = input("Enter your guess")
        if guess == answer:
            print("Well Done")
            counter = counter + 1
        else:
            print("oops")

    print("You have scored",counter,"correctly")
    return option,counter


def update_file(name, option, counter):
    string_counter = (str(counter))
    string_option = (str(option))
    results = [name,",",string_counter,",",string_option,"\n"]
    file = open('results.csv','a')
    file.writelines(results)
    file.close()

This is what it shows when the file is saved for the option variable: <_io.TextIOWrapper name='storage.csv' mode='r' encoding='cp1252'>

KCho
  • 11
  • 1
  • 3
  • Why won't you use `option='systems.csv'` in `choice()` then `with open(option, 'r') as f:` in `question()`? – Kevin Fang Aug 15 '18 at 06:54
  • Thanks Kevin, I have changed that, it now shows just as systems.csv in the file. How would I split the file name so it only shows systems? – KCho Aug 15 '18 at 07:09
  • Does that solve your problem? If so, you can search and study more about how python handles file opening and closing, then change your question to what you really want to ask, then answer the question yourself. – Kevin Fang Aug 15 '18 at 07:14
  • Not fully but it has helped in terms of what info is being saved in the file. I am trying to split the entry of the file name on the results file to only show the name of the quiz they have taken. So currently, it says systems.csv in the results file, I want it to just say systems. – KCho Aug 15 '18 at 07:17
  • Then just write `option[:-4]` or `os.path.splitext(option)[0]` instead of `option` to your final file. You need to `import os` first for the latter, which is a better (generalized) solution. – Kevin Fang Aug 15 '18 at 07:22

1 Answers1

1

You can remove the path from filename with this function:

import os
print(os.path.basename(file_name_with_path))
U3.1415926
  • 812
  • 12
  • 30