Python在目录中创建文件和删除目录

2024-09-29 21:36:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在家里练习控制评估,我发现有必要的步骤之一是为几个不同的人创建一个文件夹,如果需要重用程序,就删除他们。我试图把这些东西结合在一起,以使程序更有效,但我最终得到的结果

"Failed. Aborting operation."

输出,而不是正确地使用单数,除了我期望的目录已经存在时。另外,我正在练习的文本文件不会出现在目录中

mSelect = int(input("Menu:\n 1. Initialize system (reset or prepare)\n 2. Input scores 
\n 3. Select which contestants go through. \n 4. Give a summary of the round"))
if mSelect == 1:
    print("Initializing systems")
    import os


for x in range(0,6):
    try:
        os.makedirs("couple"+str(x+1))
        tFileLocation = ("couple"+str(x+1))
        file = open("./"+tFileLocation+"/tScore.txt","w")
        file.close()
    except:
        print("Failed to create folder for couple:",x+1)
        print("Retrying...")
        try:
            os.remove(tFileLocation)
            os.makedirs("./"+tFileLocation)
            file = open("./"+tFileLocation+"/tScore.txt","w")
            file.close()
        except:
            print("Failed. Aborting operation")

for x in range(0,5):
    try:
        os.makedirs("judge"+str(x+1))
        file
    except:
        print("Failed to create folder for judge:",x+1)
    `

上面是我用于菜单选择和程序初始化的代码。任何帮助都将不胜感激,对我所做错事的解释将是天赐之物。 提前谢谢


Tags: 程序目录forosoperationfileprinttry

热门问题