Hairo
Hairo

Reputation: 2214

make directory using variables problem

i can't make a directory using defined variables, i get an , WindowsError: [Error 183] Cannot create a file when that file already exists:

i tried something like this:

import os, ConfigParser
import Tkinter as tk

root = Tk()

exp_no = ""

config = ConfigParser.ConfigParser()
config.read("config.ini")
resultado = config.get("General", "lugar_exp")

en1 = tk.Entry(root, width = 30, background = 'white', textvariable = exp_no)
en1.pack()

os.mkdir(resultado+'/'+en1.get())

Upvotes: 2

Views: 4568

Answers (2)

samplebias
samplebias

Reputation: 37909

It sounds like Windows is raising an error because the directory already exists.

You may want to add a bit more safety by checking for existence. Also os.makedirs is a bit nicer in that it will create all missing directories on the path:

name = en1.get()
path = os.path.join(resultado, name)
if not os.path.exists(path):
    os.makedirs(path)

Upvotes: 2

Tamara Wijsman
Tamara Wijsman

Reputation: 12348

I believe that

os.mkdir(resultado+'/'+en1.get())

is running as

os.mkdir(resultado+'/')

because en1.get() might be empty or concatanation of paths is wrong which results in just resultado.

Could you verify that en1.get() contains something? And could you use os.path.join?

Upvotes: 6

Related Questions