Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
拆分Python文件名_Python_Python 3.x_Filenames - Fatal编程技术网

拆分Python文件名

拆分Python文件名,python,python-3.x,filenames,Python,Python 3.x,Filenames,我正在尝试创建一个字谜程序测验。我所做的一件事是,根据用户选择的选项,使用一种中心方法读取指定文件,而不必重复代码。但是,当尝试将信息保存到文件时,保存的变量中保存了pathfile。如何拆分它,使其仅保存已打开的文件名(即测验名) def main(): name = input("Please Enter your name to begin") print("Hi",name,"Welcome to the Computer Science Quiz") user_

我正在尝试创建一个字谜程序测验。我所做的一件事是,根据用户选择的选项,使用一种中心方法读取指定文件,而不必重复代码。但是,当尝试将信息保存到文件时,保存的变量中保存了pathfile。如何拆分它,使其仅保存已打开的文件名(即测验名)

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()
def main():
name=输入(“请输入您的姓名以开始”)
打印(“你好,姓名,“欢迎参加计算机科学测验”)
用户选择=菜单()
选项=选择(用户选择)
选项,计数器=问题(选项)
更新_文件(名称、选项、计数器)
def菜单():
打印(“从以下类别中选择:”)
打印(“1表示系统架构,2表示内存,3表示存储或4表示网络拓扑”)
choice=int(输入(“选择选项”))

如果选项>0和选项,则可以使用此函数从文件名中删除路径:

import os
print(os.path.basename(file_name_with_path))

为什么不在
choice()
中使用
option='systems.csv'
,然后在
question()
中使用open(选项'r')作为f:
?谢谢Kevin,我已经改变了这一点,它现在在文件中显示为systems.csv。如何分割文件名,使其仅显示系统?这解决了您的问题吗?如果是这样的话,您可以搜索和研究更多关于python如何处理文件打开和关闭的信息,然后将您的问题更改为您真正想问的问题,然后自己回答这个问题。虽然不完全,但它在文件中保存的信息方面有所帮助。我正在尝试分割结果文件上的文件名条目,以便只显示他们参加的测验的名称。因此,目前,结果文件中显示systems.csv,我希望它只显示systems。然后只需将
选项[:-4]
os.path.splitext(选项)[0]
而不是
选项
写入最终文件。对于后者,您需要先导入操作系统,这是一个更好的(通用的)解决方案。