#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import shutil
import logging
import datetime
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='D:\Scripts\move_file.log',
filemode='a+')
def upload_file(src_path, dst_path):
# 目标目录是否存在,不存在则创建
if not os.path.exists(os.path.dirname(dst_path)):
os.makedirs(os.path.dirname(dst_path))
# 本地文件是否存在,存在则移动到目标目录下
if os.path.exists(src_path):
shutil.move(src_path, dst_path)
def main(path):
count = 0
for root, dirs, files in os.walk(path):
for f in files:
count += 1
local_file_path = os.path.join(root, f)
upload_file(local_file_path, local_file_path.replace("xxx", "zzz"))
logging.info(str(datetime.datetime.now()) + " : " + str(count))
if __name__ == '__main__':
path = r"D:\xxx"
try:
main(path)
except Exception as e:
logging.error(e)
刚好刚写完一个。