如何建立新资料夹? [英] How to create new folder?

查看:167
本文介绍了如何建立新资料夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将程序的输出信息放置到一个文件夹中.如果给定的文件夹不存在,则程序应使用程序中给定的文件夹名称创建一个新文件夹.这可能吗?如果可以,请告诉我.

假设我给定了"C:\Program Files\alex"之类的文件夹路径,并且alex文件夹不存在,那么程序应该创建alex文件夹,并将输出信息放置在alex文件夹中.

解决方案

您可以使用 os.path .exists()来查看它是否已经存在:

newpath = r'C:\Program Files\arbitrary' 
if not os.path.exists(newpath):
    os.makedirs(newpath)

如果您要制作安装程序: Windows Installer 为您做了很多工作. /p>

I want to put output information of my program to a folder. if given folder does not exist, then the program should create a new folder with folder name as given in the program. Is this possible? If yes, please let me know how.

Suppose I have given folder path like "C:\Program Files\alex" and alex folder doesn't exist then program should create alex folder and should put output information in the alex folder.

解决方案

You can create a folder with os.makedirs()
and use os.path.exists() to see if it already exists:

newpath = r'C:\Program Files\arbitrary' 
if not os.path.exists(newpath):
    os.makedirs(newpath)

If you're trying to make an installer: Windows Installer does a lot of work for you.

这篇关于如何建立新资料夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆