跨文件夹移动文件
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_DIR)
def move_file(file, folder):
if not (os.path.exists(file) and os.path.isfile(file)):
print('文件不存在或非法')
return False
if not os.path.exists(folder):
os.makedirs(folder)
file_name = os.path.split(file)[1]
# file_name = os.path.basename(file)
new_file = os.path.join(folder, file_name)
with open(file, 'rb') as rf, open(new_file, 'wb') as wf:
for line in rf:
wf.write(line)
os.remove(file)
# 将目标文件夹下的目标文件移动到指定文件夹下
file = os.path.join(BASE_DIR, 'part5', 'mm.py')
folder = os.path.join(BASE_DIR, 'part6', 'abc')
move_file(file, folder)