文件创建者将文件夹名称识别为现有文件

时间:2019-05-16 09:48:59

标签: python

我写了一个程序,可以创建一个文件夹,文件,并返回是否创建了文件夹/文件。 例如,我在程序中创建了一个名为“ test”的文件夹,接下来,当我在菜单上选择文件创建器并输入相同的名称时,“ test”程序告诉我他认识到该文件已经存在,但实际上并不存在。 / p>


import os
import sys

#1. creating file

def creatingFile():
    try:
       fileNames = []
       nameOfFile = input("Enter the name of file: ")
       fileNames.append(nameOfFile)
       if open(nameOfFile,"x") and nameOfFile != fileNames:
           print("file created.")
           sys.exit()
    except FileExistsError:
      print("file already exist.")
      creatingFile()
    except FileNotFoundError:
        print("please enter name of your file!")
        creatingFile()

#2. creating folder
def creatingFolders():
    try:
        while True:
            nameOfFolder = input("Please enter the name of folder: ")
            if nameOfFolder != "":
                creatingFolder = os.mkdir(nameOfFolder)
                print("Your folder has been created.")
                sys.exit()
            else:
                print("You didn't write the name of your folder. You'll be back to the very beginning of folder creator.")
                continue
    except FileExistsError:
        print("Folder already exists.")
        creatingFolders()

def Menu():
    while True:
        choice = input("What you want to create? File or Folder? ")
        if choice == "folder" or choice == "FOLDER" or choice == "Folder":
            creatingFolders()
        if choice == "file" or choice == "FILE" or choice == "File":
            creatingFile()
        if choice == "":
            print("Please select what you want to select!")
            continue

Menu()```

1 个答案:

答案 0 :(得分:0)

在Windows中,您无法创建具有相同名称的文件和文件夹,因此test 已经存在。