python移动文件到另外一个目录

代码:

<pre># coding=utf-8
import os
import shutil
import traceback


def move_file(src_path, dst_path, file):
    print
    'from : ', src_path
    print
    'to : ', dst_path
    try:
        # cmd = 'chmod -R +x ' + src_path
        # os.popen(cmd)
        f_src = os.path.join(src_path, file)
        if not os.path.exists(dst_path):
            os.mkdir(dst_path)
        f_dst = os.path.join(dst_path, file)
        shutil.move(f_src, f_dst)
    except Exception as e:
        print
        'move_file ERROR: ', e
        traceback.print_exc()


src_path= '/xacisco-blog/文章'
dst_path= '/xacisco-blog/文章/已经读取'
move_file(src_path,dst_path,'测试文件.txt')

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注