Question

Extremely new to Python, just got out of a very basic training course and delving into my first application I'm using python3 and i have been trying to get itertools and os.mkdirs() to create a standard directory structure under a user defined directory the user states the directory name, that variable is called "asset" which is entered from a tk.Entry line, the code should generate the user directory and 6 sub directories under the path "P:\projects_2013\" The error is that the sub directories are being created directly under the PATH rather than under the 'asset' sub directory which is baffling me, i'm sure its just syntax but i cant see the wood for the trees!! any ideas...

here's the code:

import tkinter as tk
import getpass
import os, sys
import itertools

path = ("P:\\Projects_2013\\")
#create new window
root = tk.Tk()
#set window title
root.title("Toolkit")
#set window size
root.geometry("600x600+200+200")
#set window icon
root.wm_iconbitmap('Cartoon_Robot_200.ico')


#add dir_creator as widget
def directory():
    directory = path
    if directory:
       path.set(directory)


def genAsset():
    asset_name = asset.get()
    os.chdir(path)
    dirs = [[asset_name],["subdir1", "subdir2", "subdir3", "subdir4", "subdir5", "subdir6"]]
    for item in itertools.product(*dirs):
        os.makedirs(os.path.join(*item))

asset = tk.StringVar()
#wrtuser = tk.StringVar()

#wrtuser_label = tk.Label(root.text=("Username =").grid(row=2, column=1)
#wrtuser_entry = tk.Entry(root, textvariable=wrtuser, width=50).grid(row=2, column=2)
asset_label = tk.Label(root, text="Create New Project:").grid(row=3)
asset_entry = tk.Entry(root, textvariable=asset, width=50).grid(row=3, column=2)
create_button = tk.Button(root, text="Create Folder", command=genAsset).grid(row=4, column=3)
dir_label = tk.Label(root, text="The project directory will be created in P:\Projects_2013\\").grid(row=4, columnspan=4)


#draw window and start application
root.mainloop()
Was it helpful?

Solution

In line 26, you are changing your current working directory to the root directory which seems to be "P:\Projects_2013\"

You need to change line to 26 to this:

os.chdir(os.path.join(path, asset_name))

Also, note that your code will fail if P:\Projects_2013\ doesn't exist. Are you sure it'll exist for everyone? Are you also sure everyone is mapping this drive where Projects_2013 exists to P? If not, you might want to use UNC instead

Hope this helps!

OTHER TIPS

well, this is embarrassing.....

Asked a colleague to fire up a piece of Python code which was failing on my machine, only for it to work on his !??? created myself a new user account on our network and got this code working without issue, turns out my user account had got some sort of corruption ! :/

The initial code i posted does in fact work and creates a parent directory and sub directories.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top