Python 使用给定路径创建一个新文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42817898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a new Folder with given path
提问by Victor Aguilar
I'm wondering if any of you have created a function to create a folder within a given path.
我想知道你们中是否有人创建了一个函数来在给定路径中创建一个文件夹。
For Example: NewFolder = ['/projects/Resources/backup_Folder']
例如:NewFolder = ['/projects/Resources/backup_Folder']
回答by Adam Hughes
Use the os
library, specifically os.mkdir()
使用os
图书馆,特别是os.mkdir()
https://docs.python.org/2/library/os.html#os.mkdir
https://docs.python.org/2/library/os.html#os.mkdir
For example,
例如,
path = "/usr/temp/foo"
os.mkdir(path)
If the intermediate folders don't exists, use os.makdirs
as per Peter Wood's comment
如果中间文件夹不存在,请os.makdirs
按照 Peter Wood 的评论使用
path = "/newfolder1/newfolder2/foo"
os.mkdir(path)