简体   繁体   中英

Makedirs is creating a file not a folder

I am having an issue where when I try to make a folder it makes a file instead. Specifically it occurs in the variable folder. Code is as follows and I am using 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")

On my computer I get this error

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

What am I doing wrong? I cannot seem to figure it out

You never create the folder, in

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

You are trying to create the folder if os.path.exists throws an exception, but it doesn't, it return True or False , and later on you create the file in

k = open(full, "w")

Use it like that

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

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

I don't know what you are implementing with this code, but I've made some changes to your code. I think this will help you.

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()

The problem is your code does not create a directory when it does not exist. Consider:

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

In the try block, you call os.path.exists(folder) , but that does not generate any exception. That means the code in the except clause does not get executed. What you want is to create the directory regardless that it exists and just ignore the error:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM