Python - 我如何要求用户在他们的桌面上创建一个文件夹?
Python - How would I require a user to create a folder on their desktop?
我有一个包含 GUI 的应用程序。当用户单击 'Submit' 时,我想检查并确保他们在桌面上创建了一个具有特定名称的文件夹。如果他们没有在他们的桌面上创建这个文件夹,我想用一个弹出窗口提示他们,通知他们创建这个文件夹。我知道如何创建弹出窗口,但我将如何检查他们是否创建了这个文件夹?让我们称这个文件夹为 'example'。
我试过:
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()
这是这样做的一个小例子: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()
我有一个包含 GUI 的应用程序。当用户单击 'Submit' 时,我想检查并确保他们在桌面上创建了一个具有特定名称的文件夹。如果他们没有在他们的桌面上创建这个文件夹,我想用一个弹出窗口提示他们,通知他们创建这个文件夹。我知道如何创建弹出窗口,但我将如何检查他们是否创建了这个文件夹?让我们称这个文件夹为 'example'。
我试过:
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()
这是这样做的一个小例子: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()