Python新建/删除文件夹
2016-05-13 09:52
477 查看
新建以当前日期为名的文件夹
复制文件:
shutil.copyfile("oldfile","newfile") oldfile和newfile都只能是文件
shutil.copy("oldfile","newfile") oldfile只能是文件夹,newfile可以是文件,也可以是目标目录
复制文件夹:
shutil.copytree("olddir","newdir") olddir和newdir都只能是目录,且newdir必须不存在
重命名文件(目录)
os.rename("oldname","newname") 文件或目录都是使用这条命令,oldname和newname都要是filepath
移动文件(目录)
shutil.move("oldpos","newpos")
删除文件
os.remove("file")
转换目录
os.chdir("path") 换路径
判断是否存在
os.path.isdir("aa") 判断目标是否目录
os.path.isfile("bb.txt") 判断目标是否文件
import datetime,os,shutil today = datetime.datetime.now().date().strftime('%Y%m%d') pathh= os.path.join('e:\\a','bb',today ) #可以连接多级目录 print pathh #返回 e:\a\bb\20160513 if not os.path.exists(pathh): os.makedirs(pathh) #可以创建多级不存在的目录 os.makedir(pathh)只能创建单级目录 os.rmdir(pathh) #只能删除20160513目录,e:\a\bb还存在 shutil.rmtree('e:\\a') #空目录、有内容的目录都可以删除
复制文件:
shutil.copyfile("oldfile","newfile") oldfile和newfile都只能是文件
shutil.copy("oldfile","newfile") oldfile只能是文件夹,newfile可以是文件,也可以是目标目录
复制文件夹:
shutil.copytree("olddir","newdir") olddir和newdir都只能是目录,且newdir必须不存在
重命名文件(目录)
os.rename("oldname","newname") 文件或目录都是使用这条命令,oldname和newname都要是filepath
移动文件(目录)
shutil.move("oldpos","newpos")
删除文件
os.remove("file")
转换目录
os.chdir("path") 换路径
判断是否存在
os.path.isdir("aa") 判断目标是否目录
os.path.isfile("bb.txt") 判断目标是否文件
相关文章推荐
- Python之路,Day1
- michael的Python笔记(三)
- Python简易爬虫--抓取任意数目百度百科内容
- 【python】零碎总结_转
- python基于phantomjs实现导入图片
- 如何查看Python的内置函数
- Python - 动手写个ORM
- Python 依赖库
- Python笔记:除、取整、取余、乘方
- Python笔记:加减乘除,格式化字符串
- ipython的两种安装方式
- 《Python核心编程》第7章 习题
- Python-continue的使用和if的位置
- ubuntu为Python添加默认搜索路径
- Python:统计系统剩余内存
- python基于phantomjs实现导入图片
- Python生成图片验证码
- 使用 Python 切割图片
- python subprocess模块
- python gevent 模块