[英]Python - How would I require a user to create a folder on their desktop?
我有一个包含 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()
这是这样做的一个小例子: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.