python 复制文件到其他路径

#!/usr/bin/env/python
# -*- coding: utf-8 -*-
# @Time    : 2018/11/7 16:34
# @Author  : ChenAdong
# @Email   : aiswell@foxmail.com

import os
import logging
import shutil


logging.basicConfig(level=logging.WARN)


def txt_reader(_path):
	f = open(_path, "r")
	_type = ""
	for line in f.readlines():
		if line.split("|")[0] == "tokentype":
			tem = line.split("|")[1]
			tem = tem.strip()
			# tem = int(tem)
			if tem == "1.0":
				_type = "admin"
			elif tem == "2.0":
				_type = "c"
			elif tem == "3.0":
				_type = "a"
			else:
				logging.warning("%s 未找到tokentype字段" % _path)
	logging.debug("%s %s" % (_path, _type))
	f.close()
	return _path, _type


def move_to(from_dir, to_dir):
	# tem = from_dir.spilt("/")
	# for _file in tem:
	# 	if os.path.isdir(_file):
	# 		pass
	# 	else:
	shutil.copyfile(from_dir, "./%s/%s" % (to_dir, from_dir))


if __name__ == "__main__":
	for i in os.listdir(os.getcwd()):
		try:
			if os.path.isfile(i):
				_path, _type = txt_reader(i)
				# to_dir = "./%s/%s" % (_type, _path)
				move_to(_path, _type)
		except shutil.Error:
			logging.warning("%s 文件被忽略" % _path)

  

原文地址:https://www.cnblogs.com/chenadong/p/10043393.html