简体   繁体   English

Python - 我如何要求用户在他们的桌面上创建一个文件夹?

[英]Python - How would I require a user to create a folder on their desktop?

I have an application that includes a GUI.我有一个包含 GUI 的应用程序。 When the user clicks 'Submit' I would like to check and make sure that they have created a folder on their desktop with a specific name.当用户单击“提交”时,我想检查并确保他们在桌面上创建了一个具有特定名称的文件夹。 If they have not created this folder on their desktop I would like to prompt them with a popup informing them to create this folder.如果他们尚未在桌面上创建此文件夹,我想通过弹出窗口提示他们创建此文件夹。 I know how to create popups but how would I go about checking that they have created this folder?我知道如何创建弹出窗口,但我将如何检查他们是否创建了这个文件夹? Lets call this folder 'example'.让我们将此文件夹称为“示例”。

I have tried:我试过了:

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

This is a small example of doing this :D这是这样做的一个小例子: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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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