python 怎么将输入目录内的文件拷贝至另一个目录的同名文件夹? 用python把文件夹下的所有文件包括文件夹里面的文件都拷贝...

\u5982\u4f55\u4f7f\u7528python\u4ee3\u7801\uff0c\u4ece\u5f53\u524d\u6587\u4ef6\u5939\u4e00\u4e2a\u6587\u4ef6\u91cc\u590d\u5236\u5b57\u7b26\u5230\u53e6\u4e00\u4e2a\u6587\u4ef6\u5939\u4e0b\u7684\u540c\u540d\u6587\u4ef6\u91cc\uff0c\u6587\u4ef6\u6709\u591a\u4e2a\uff01

import os# \u53c2\u6570\u8bbe\u7f6e# \u81ea\u884c\u5b9a\u4e49\u6e90\u6587\u4ef6\u5730\u5740\u548c\u76ee\u6807\u5730\u5740_TARGET_DIR = "./copied_files/"_SOURCE_DIR = "./source_files/"# \u4f60\u81ea\u5df1\u5b9a\u4e49\u7684\u63d0\u53d6\u7279\u5b9a\u4fe1\u606f\u7684\u65b9\u6cd5\uff1adef extract(filename): # \u63d0\u53d6\u4fe1\u606f\u540e\u8fd4\u56de with open(filename, "r") as f: info = f.readlines() return info# \u4f7f\u7528os.listdir()\u65b9\u6cd5\u83b7\u53d6\u6e90\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u6587\u4ef6# \u6709\u65f6\u7cfb\u7edf\u5185\u4f1a\u6709\u4e9b\u9690\u85cf\u6587\u4ef6\u4ee5"."\u5f00\u5934\uff0c\u9700\u8981\u5254\u9664files = [file for file in os.listdir(_SOURCE_DIR) if not file.startswith(".")]for filename in files: # 1.\u8bfb\u53d6\u6587\u4ef6\u5e76\u63d0\u53d6\u4fe1\u606f\uff1a print("\u6b63\u5728\u5904\u7406{}...".format(filename)) info = extract(_SOURCE_DIR + filename) # 2.\u5728\u76ee\u6807\u6587\u4ef6\u5939\u521b\u5efa\u540c\u540d\u6587\u4ef6\uff0c\u5e76\u5c06\u4fe1\u606f\u5199\u5165 # \u5199\u5165\u90e8\u5206\u6216\u9700\u8981\u6839\u636e\u9700\u8981\u8c03\u6574 with open(_TARGET_DIR + filename, "w") as f: for line in info: f.write(line) print("\u5904\u7406\u5b8c\u6210\uff01")

def change(path, path1): for f in os.listdir(path): if os.path.isfile(path + os.path.sep + f): a, b = os.path.splitext(f) if b != '.py': shutil.copy(path + os.sep + f, path1) elif os.path.isdir(path + os.path.sep + f): change(path + os.sep + f, path1)if __name__ == '__main__': path = 'D:\\workspace\\python' path1 = 'D:\\workspace\\python\\filepath' change(path, path1)\u4f60\u597d\uff0c\u6211\u628achange\u7a0d\u5fae\u6539\u4e86\u4e00\u4e0b\uff0c\u770b\u770b\u884c\u4e0d

这是最近写的一个类似代码,你拿去改改
import shutil
import os
import logging
import sys

logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

def cp_or_mv2(src_file, des_dir, is_copy):
print(src_file, des_dir)
if os.path.isfile(src_file):
logger.info(f'from file {src_file}')
if is_copy:
shutil.copy2(src_file, des_dir)
logger.info(f'copy to {des_dir}')
else:
des_file = os.path.join(des_dir, src_file)
shutil.move(src_file, des_file)
logger.info(f'move to {des_file}')
else:
logger.info(f'from dir {src_file}')
des_dir_level1 = os.path.join(des_dir, src_file)
shutil.copytree(src_file, des_dir_level1, dirs_exist_ok=True)
logger.info(f'to {des_dir_level1}')
if not is_copy:
shutil.rmtree(src_file)
logger.info(f'deleted {src_file}')

def process_files_in_txt(txt_file, src_dir, des_dir, is_copy=True):
os.chdir(src_dir)
with open(txt_file, 'r', encoding='utf8', errors='ignore') as f:
for line in f.readlines():
src_file = line.strip()
# logger.info(src_file)
if os.path.exists(src_file):
cp_or_mv2(src_file, des_dir, is_copy)
else:
logger.warning(f'{src_file} missing!')

if __name__ == '__main__':
process_files_in_txt(r"D:\D\需要拷贝.txt", # 哪些文件(夹)
r"D:\D\Desktop", # 从哪个文件夹
r"D:\D\新建文件夹", # 到哪个文件夹
is_copy=False) # True复制,False剪切

把这个脚本放到eew文件夹里运行:
import os
from shutil import copy
for root, dirs, files in os.walk('data'):
for f in files:
file_name = f[:f.index('.')]
copy(os.path.join(root, f), os.path.join('datum', file_name))

扩展阅读:python根目录在哪里找 ... python解释器手机版下载 ... python基础代码大全 ... python读取文件目录 ... python进入指定目录 ... python怎么进入目录 ... python的安装目录在哪里 ... python的根目录在哪 ... 怎么找到python的安装目录 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网