【问题标题】:Makedirs is creating a file not a folderMakedirs 正在创建文件而不是文件夹
【发布时间】:2019-11-24 06:21:16
【问题描述】:

我遇到了一个问题,当我尝试创建一个文件夹时,它会创建一个文件。具体来说,它出现在变量文件夹中。代码如下,我使用的是python 2.7

import os

i = False
while i != True:
    fil = raw_input("Enter Filename Here: ")
    i = os.path.exists(fil)
    if i == False: print("File does not exist! Try Again.")

folder = raw_input("Enter Output Folder Here: ")
try:
    os.path.exists(folder)
except:
    if not os.path.exists(folder):
        print("Creating Folder for You.")
        os.makedirs(folder)

output = raw_input("Enter Chlorophyll-a Output Filename Here: ")
full = os.path.join(folder, output)

if os.path.exists(full):
    yesno = raw_input("Output file already exists are you sure you want to overwrite? Yes/No: ")
    if yesno == "Yes":
        k = open(full, "w")
    if yesno == "No":
        raise SystemExit("Exiting File Now!")
if not os.path.exists(full):
    print ("File Does Not Exist. I Will Make It For You.")
    k = open(full, "w")

在我的电脑上出现此错误

Traceback (most recent call last):
  File "C:/Users/---/.PyCharmCE2019.2/config/scratches/scratch_6.py", line 33, in <module>
    k = open(full, "w")
IOError: [Errno 2] No such file or directory

我做错了什么?我似乎无法弄清楚

【问题讨论】:

  • “它会生成文件”是什么意思? os.makedirs 无法创建文件。它是在制作一个具有文件扩展名的文件夹吗?
  • 由于某种原因,当我运行此代码时,它根本没有创建文件夹,而只是一个绝对不是文件夹目录的空文件。它根本不会创建一个全新的文件夹,只是一个空文件。
  • 你在运行脚本时输入了什么路径?
  • 我的输入只是一个 excel xlsm 文件 C:/Users/---/documents/excelfile.xlsx。对于我的输出文件夹,我做了 C:/Users/---/Desktop/output。其中输出是一个不存在的文件夹,我想创建一个。
  • 您似乎误解了 os.path.exists 的工作原理。它返回 True 或 False 取决于你给它的路径是否已经存在。它不会抛出异常,因此您创建文件夹的 except: 子句永远不会实际执行。

标签: python input directory io


【解决方案1】:

你永远不会创建文件夹,在

try:
    os.path.exists(folder)
except:
    if not os.path.exists(folder):
        print("Creating Folder for You.")
        os.makedirs(folder)

如果os.path.exists 抛出异常,您正在尝试创建文件夹,但它没有,它返回TrueFalse,稍后您在其中创建文件

k = open(full, "w")

这样使用

folder = raw_input("Enter Output Folder Here: ")

if not os.path.exists(folder):
    print("Creating Folder for You.")
    os.makedirs(folder)

【讨论】:

    【解决方案2】:

    我不知道你用这段代码实现了什么,但我对你的代码做了一些更改。我想这会对你有所帮助。

    import os
    
    fil = input("Enter Filename Here: ")
    if not os.path.exists(fil):
        print("File doesn't exist!")
    
        folder = input("Enter Output Folder Here: ")
        if not os.path.exists(folder):
            print("Creating folder for you.")
            os.makedirs(folder)
    
        output = input("Enter Chlorophyll-a Output Filename Here: ")
        full = os.path.join(folder, output)
    
        if os.path.exists(full):
            yesno = input("Output file already exists are you sure you want to overwrite? Yes/No: ")
            if yesno == "Yes":
                k = open(full, "w")
                k.close()
            if yesno == "No":
                raise SystemExit("Exiting File Now!")
        else:
            print ("File Does Not Exist. I Will Make It For You.")
            k = open(full, "w")
            k.close()
    

    【讨论】:

      【解决方案3】:

      问题是当目录存在时,您的代码不会创建目录。考虑:

      try:
          os.path.exists(folder)
      except:
          if not os.path.exists(folder):
              print("Creating Folder for You.")
              os.makedirs(folder)
      

      在 try 块中,您调用 os.path.exists(folder),但这不会产生任何异常。这意味着 except 子句中的代码不会被执行。你想要的是创建目录,不管它是否存在,而忽略错误:

      try:
          os.makedirs(folder)
      except OSError:
          pass
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-07
        • 2019-10-14
        • 2013-04-19
        相关资源
        最近更新 更多