python文件、文件夹操作
2018-03-27 21:53
309 查看
文件相关创建操作见:http://www.runoob.com/python/python-files-io.html
文件夹操作使用os
判断文件是否存在import os
import shutil
os.remove(path) #删除文件
os.removedirs(path) #删除空文件夹
os.mkdir(path) #创建文件夹
os.makedirs(path)#级联创建文件夹
shutil.rmtree(path) #递归删除文件夹
#先定义一个带路径的文件filename = "/home/mydir/test.txt"
#将文件路径分割出来file_dir = os.path.split(filename )[0]#判断文件路径是否存在,如果不存在,则创建,此处是创建多级目录
if not os.path.isdir(file_dir):
os.makedirs(file_dir)#然后再判断文件是否存在,如果不存在,则创建
if not os.path.exists(filename ):
os.system(r'touch %s' % filename)
文件夹操作使用os
判断文件是否存在import os
import shutil
os.remove(path) #删除文件
os.removedirs(path) #删除空文件夹
os.mkdir(path) #创建文件夹
os.makedirs(path)#级联创建文件夹
shutil.rmtree(path) #递归删除文件夹
#先定义一个带路径的文件filename = "/home/mydir/test.txt"
#将文件路径分割出来file_dir = os.path.split(filename )[0]#判断文件路径是否存在,如果不存在,则创建,此处是创建多级目录
if not os.path.isdir(file_dir):
os.makedirs(file_dir)#然后再判断文件是否存在,如果不存在,则创建
if not os.path.exists(filename ):
os.system(r'touch %s' % filename)
相关文章推荐
- Python文件夹与文件的操作
- python对文件,文件夹的一些操作
- Python文件夹与文件的操作
- Python文件夹与文件的操作
- python 文件、文件夹和路径操作笔记
- Python:文件夹与文件的操作
- python文件+文件夹操作
- python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块
- Python--文件及文件夹操作
- Python文件夹与文件的操作
- Python文件夹与文件的操作
- python中对文件、文件夹的操作需要涉及到os模块和shutil模块。
- Python文件/文件夹操作大全
- 转载_Python文件夹与文件的操作
- Python文件夹与文件的操作 分类: python 2013-05-21 15:33 310人阅读 评论(0) 收藏
- python中对文件、文件夹的操作
- Python文件夹与文件的操作
- python文件夹遍历,文件操作,获取文件修改创建时间<转>
- Python文件夹与文件的操作
- python中对文件、文件夹的操作