Python创建系统目录的方法
作者:泥人张 时间:2023-11-22 11:52:47
本文实例讲述了Python创建系统目录的方法。分享给大家供大家参考。具体如下:
Python2 mkdir在没有上级目录时创建会失败.该方法可以创建多级目录。
/temp/gapgers/upload/images/1.png
如过temp文件夹不存在,会创建空的文件夹/temp/gapgers/upload/images/以及空文件1.png。
该方法只做抛砖引玉,大神勿喷
import os
def mkfilePower(path):
'''create dirs if the path contain a file create a empty file
if the dir's file is exist return False else return True
ex:path = r'c:/temp/gapgers/upload/images/1.png'
nomatter there have dir temp or not,we will create it and create a empty file 1.png
'''
paths = path.split('/')
temppath = ''
for index,_spilt in enumerate(paths):
if index == 0:
temppath = _spilt
continue
temppath = temppath + '/' + _spilt
if os.path.isdir(temppath):
pass
elif index == len(paths)-1:
if os.path.isfile(temppath):
return False
fl = open(temppath,'w')
fl.close()
else:
os.mkdir(temppath)
return True
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,目录,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mysql忘记root密码怎么办
2024-01-19 02:29:22
golang grpc配置使用实战
2024-02-06 01:17:09
![](https://img.aspxhome.com/file/2023/5/109735_0s.png)
Python中filter与lambda的结合使用详解
2022-03-03 01:35:03
python验证多组数据之间有无显著差异
2021-03-12 15:37:22
![](https://img.aspxhome.com/file/2023/4/120554_0s.png)
asp如何用FileSystemObject组件来做一个站内搜索?
2010-06-12 12:47:00
利用python为PostgreSQL的表自动添加分区
2023-07-07 14:44:58
Python脚本实现自动发带图的微博
2021-04-18 08:37:01
![](https://img.aspxhome.com/file/2023/4/100264_0s.png)
微信小程序实现图片上传功能
2023-09-06 13:08:44
详解在SQLPlus中实现上下键翻查历史命令的功能
2024-01-27 01:51:36
探讨Oracle中的&号问题
2024-01-22 09:13:31
添加到各大流行网摘 书签的代码
2008-04-20 14:15:00
鼠标实现图片的渐有渐无
2013-06-30 02:49:10
ASP长文章分页代码实例
2007-10-02 17:04:00
SQL 查询连续登录的用户情况
2024-01-29 01:27:18
![](https://img.aspxhome.com/file/2023/0/104110_0s.png)
详谈python3中用for循环删除列表中元素的坑
2023-08-01 06:04:31
mysql创建表设置表主键id从1开始自增的解决方案
2024-01-18 13:52:53
![](https://img.aspxhome.com/file/2023/6/67076_0s.png)
一看就懂得Python的math模块
2021-03-28 21:40:32
sql获取分组排序后数据的脚本
2024-01-20 09:07:58
全面理解JavaScript中的闭包
2024-04-17 10:09:24
![](https://img.aspxhome.com/file/2023/2/136352_0s.png)
修改mysql密码与忘记mysql密码的处理方法
2024-01-23 21:53:48