经常用到文件的基本操作,python简单实现一把;
Talk is cheap,show me the code!
import shutil
import os
import pandas as pd
#所有的src需要为绝对路径!!!
#删除文件或者空文件夹-----------------
def rm_free_dir(src):
#删除空文件夹;
if os.path.isdir(src):
os.rmdir(src)
else:
#删除文件
os.remove(src)
#删除非空的文件夹--------------------
def rm_nofree_dir(src):
if os.path.exists(src):
shutil.rmtree(src)
#移动文件----------------
def move_files(src, dst):
dirname = os.path.dirname(dst)
#判断文件夹是否存在,不存在就创立一个
if not os.path.exists(dirname):
os.makedirs(dirname, exist_ok=True)
#判断目标文件是否存在,不存在则移动过来
if not os.path.exists(dst):
shutil.move(src, dst)
#拷贝文件-------------------
def copy_files(src, dst):
dirname = os.path.dirname(dst)
#判断文件夹是否存在,不存在就创立一个
if not os.path.exists(dirname):
os.makedirs(dirname, exist_ok=True)
#判断目标文件是否存在,不存在则拷贝过来
if not os.path.exists(dst):
shutil.copy(src, dst)
有问题随时联系,欢迎一键三连~