【问题标题】:Python - How would I require a user to create a folder on their desktop?Python - 我如何要求用户在他们的桌面上创建一个文件夹?
【发布时间】:2020-04-28 03:57:39
【问题描述】:

我有一个包含 GUI 的应用程序。当用户单击“提交”时,我想检查并确保他们已在桌面上创建了一个具有特定名称的文件夹。如果他们没有在桌面上创建此文件夹,我想通过弹出窗口提示他们创建此文件夹。我知道如何创建弹出窗口,但我将如何检查他们是否创建了此文件夹?让我们将此文件夹称为“示例”。

我试过了:

desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 
   'Desktop').replace('\\', '/')

   if 'Example' not in desktop:
            sg.popup("Please create a folder named 'Example' on Desktop and 
            resubmit")
            quit()

【问题讨论】:

  • 请详细说明您到目前为止所做的尝试。
  • 可以获取文件夹列表。通过它你可以检查它。
  • 为什么你的程序不自己创建文件夹?
  • 我会选择创建文件夹本身,但我无法完全控制这个决定。

标签: python directory directory-structure


【解决方案1】:

这是一个小例子:D

import os
def makeDir():
    folder="exemple"
    print os.path.expanduser("~") # path to the home directory of the user
    path_to_user=os.path.expanduser("~")
    path_to_desktop=os.path.join(path_to_user,"Desktop") #we are now in Desktop folder
    if os.path.isdir(os.path.join(path_to_desktop,folder)):
        return False #if exists ....
    else:
        #os.mkdir(os.path.join(path_to_desktop,folder)) #if not, make dir without asking or 
        return True

if makeDir()==False:
    print "Exists!"
    #continue to do smt
else:
    print "Make that dir!" # make the require

makeDir()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 2016-07-11
    • 2021-02-27
    相关资源
    最近更新 更多