Python之文件操作

Github:https://github.com/huangshiyu13/PythonFileLib

1.获得文件夹下所有文件名

for file in os.listdir(imageDir):
    file_path = os.path.join(imageDir, file) 
    if os.path.isfile(file_path) and os.path.splitext(file_path)[1]=='.rmvb':

        print file

2.判断文件夹是否存在,如果不存在则创建,如果存在则删除后创建

outDir = "outDir";

if os.path.isdir(outDir):

    shutil.rmtree(outDir)

os.mkdir(outDir)

3.判断文件是否存在

import os 
os.path.isfile('test.txt') #如果不存在就返回False 

4.复制文件

def copyFile(source, target):

    open(target, "wb").write(open(source, "rb").read())

原文地址:https://www.cnblogs.com/huangshiyu13/p/5891897.html