python复制移动文件_python常用的几种文件操作,复制,移动,打包

# -*- coding: UTF-8 -*-

import os

import shutil

import traceback

import pypinyin

from PIL import Image

from globalLog import ta_log

def copy_file(srcfile, dstfile):

if not os.path.isfile(srcfile):

# 一般来说,是因为复制的不是文件所导致的,不影响

ta_log.info("%s not file!" % (srcfile))

else:

fpath, fname = os.path.split(dstfile) # 分离文件名和路径

if not os.path.exists(fpath):

os.makedirs(fpath) # 创建路径

shutil.copy2(srcfile, dstfile) # 移动文件

ta_log.info("copy %s -> %s" % (srcfile, dstfile))

def movefile(srcfile, dstfile):

if not os.path.isfile(srcfile):

ta_log.error("%s not exist!" % (srcfile))

else:

fpath, fname = os.path.split(dstfile) # 分离文件名和路径

if not os.path.exists(fpath):

os.makedirs(fpath) # 创建路径

shutil.move(srcfile, dstfile) # 移动文件

ta_log.info("move %s -> %s" % (srcfile, dstfile))

def copy_file_to_capture(srcfile):

'''

复制到capture目录,并且把汉字改成拼音

:param srcfile:

:return:

'''

fpath, fname = os.path.split(srcfile)

filename_pre_ = pypinyin.slug(fname)

dstfile = os.path.abspath(os.getcwd() + "/filepath/attachement/capture/" + filename_pre_)

copy_file(srcfile, dstfile)

def resize_mage(filein):

img = Image.open(filein)

out = img.resize((129, 149), Image.ANTIALIAS) # resize image with high-quality

out.save(filein, 'png')

# 只能用于删除本地文件

def delete_many_file(file_list):

for path in file_list:

if os.path.exists(path):

# 删除文件,可使用以下两种方法。

try:

os.remove(path)

except Exception:

ta_log.error(traceback.format_exc())

# os.unlink(my_file)

def copy_and_zip(file_list, dst_folder_name):

'''

批量复制文件到指定文件夹,然后把指定文件夹的内容压缩成ZIP并且删掉该文件夹

:param file_list: 文件或文件夹

:param dst_folder_name: 目标压缩文件的名称

:return:

'''

for item in file_list:

copy_files_to_attachment(item, dst_folder_name)

source = os.getcwd() + "/filepath/attachement/" + dst_folder_name

shutil.make_archive(source, "zip", source)

shutil.rmtree(source)

def copy_files_to_attachment(srcfile, filename):

'''

把文件或文件夹复制到指定目录中

:param srcfile: 文件或者文件夹的绝对路径

:param filename: 指定目录

:return:

'''

dstfile = os.path.abspath(os.getcwd() + "/filepath/attachement/")

folder_name = dstfile + "\\" + filename + "\\"

if not os.path.isfile(srcfile):

last_name = os.path.basename(srcfile)

destination_name = folder_name + last_name

shutil.copytree(srcfile, destination_name, symlinks=True)

else:

fpath, fname = os.path.split(folder_name) # 分离文件名和路径

if not os.path.exists(fpath):

os.makedirs(fpath) # 创建路径

shutil.copy2(srcfile, folder_name) # 移动文件

print("copy %s -> %s" % (srcfile, folder_name))

def copy_file_to_folder(srcfile, folder_name):

'''

把文件或文件夹复制到指定目录中

:param srcfile: 文件或者文件夹的绝对路径

:param filename: 指定目录

:return:

'''

if not os.path.isfile(srcfile):

last_name = os.path.basename(srcfile)

destination_name = folder_name + last_name

shutil.copytree(srcfile, destination_name, symlinks=True)

else:

fpath, fname = os.path.split(folder_name) # 分离文件名和路径

if not os.path.exists(fpath):

os.makedirs(fpath) # 创建路径

shutil.copy2(srcfile, folder_name) # 移动文件

print("copy %s -> %s" % (srcfile, folder_name))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值