0

对 Python 非常陌生,刚刚完成了一个非常基础的培训课程并深入研究了我正在使用 python3 的第一个应用程序,我一直在尝试使用 itertools 和 os.mkdirs() 在用户定义的目录下创建标准目录结构用户说明目录名称,该变量称为“asset”,从 tk.Entry 行输入,代码应在路径“P:\projects_2013\”下生成用户目录和 6 个子目录。错误是子目录是直接在 PATH 下创建的,而不是在令我困惑的“资产”子目录下,我确定它只是语法,但我看不到树木的木材!!有任何想法吗...

这是代码:

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

2 回答 2

0

在第 26 行,您将当前工作目录更改为似乎是“P:\Projects_2013\”的根目录

您需要将第 26 行更改为:

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

另外,请注意,如果 P:\Projects_2013\ 不存在,您的代码将失败。你确定它对每个人都存在吗?您还确定每个人都将 Projects_2013 所在的驱动器映射到 P 吗?如果没有,您可能想改用UNC

希望这可以帮助!

于 2013-10-29T16:44:00.100 回答
0

好吧,这很尴尬......

请一位同事启动一段在我的机器上失败的 Python 代码,只是为了让它在他的机器上工作!???在我们的网络上为自己创建了一个新的用户帐户并让此代码正常工作,结果我的用户帐户出现了某种损坏!:/

我发布的初始代码确实有效,并创建了父目录和子目录。

于 2013-11-04T11:10:43.537 回答