Python-我将如何要求用户在其桌面上创建文件夹?

问题描述 投票:-2回答:1

我有一个包含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个回答
0
投票

这是执行此操作的小例子: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()
© www.soinside.com 2019 - 2024. All rights reserved.