日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【Python】shutil内置模块复制和重命名文件

發布時間:2025/3/15 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python】shutil内置模块复制和重命名文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在日常工作和生活中,我們經常要復制和重命名文件,如果遇到大量數據處理時,手動去操作非常麻煩,現在我們可以通過python的shutil模塊完成,以下主要介紹幾種場景:
1.復制一個文件到其他目錄,不重新命名;
2.復制一個文件到其他目錄,重新命名;
3.復制某個固定文件,生成N個重命名的新文件;
4.復制某個文件夾中多個文件,到其他目錄、不重新命名;
5.復制某個文件夾中多個文件,到其他目錄、重新命名;

# -*- coding: utf-8 -*- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #作者:cacho_37967865 #博客:https://blog.csdn.net/sinat_37967865 #文件:rename_file.py #日期:2020-05-02 #備注: 批量對文件進行重命名、批量復制文件 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''import os import shutil from pythonlession.basefunction.get_all_file import get_files from pythonlession.basefunction.deal_file import get_file,save_file# 復制某個文件:sample-原始文件、new_path-新文件目錄 def copy_file(sample,new_path):if not os.path.exists(new_path):os.makedirs(new_path)new_file = os.path.join(new_path, os.path.basename(sample))shutil.copy(sample, new_file)print('復制后的文件完整路徑:',new_file)return new_file# 復制某個文件并重命名:sample-原始文件、new_path-新文件目錄、file_name-新文件名稱 def copy_rename_file(sample,new_path,file_name):if not os.path.exists(new_path):os.makedirs(new_path)new_file = os.path.join(new_path, file_name)shutil.copy(sample, new_file)print('復制并重命名后的文件完整路徑:',new_file)return new_file# 復制某個文件生成多個重命名文件:sample-原始文件、new_path-新文件目錄、num-生成數量 def copy_n_files(sample,new_path,num):if not os.path.exists(new_path):os.makedirs(new_path)for i in range(num):new_file = os.path.join(new_path, '%03d_'%(i) + os.path.basename(sample))shutil.copy(sample, new_file)print('復制并重命名后的文件完整路徑:',new_file)# 復制多個文件到其他目錄: def copy_files(file_path,file_type,new_path):if not os.path.exists(new_path):os.makedirs(new_path)files = get_files(file_path, file_type)for file in files:new_file = os.path.join(new_path, os.path.basename(file))shutil.copy(file, new_file)print('復制后的文件完整路徑:', new_file)# 復制多個文件到其他目錄并且重命名:方法一 def copy_rename_files(file_path,file_type,new_path):if not os.path.exists(new_path):os.makedirs(new_path)files = get_files(file_path, file_type)for i in range(len(files)):file = files[i]new_file = os.path.join(new_path, '%03d_'%(len(files)-i) + os.path.basename(file))shutil.copy(file, new_file)print('復制并重命名后的文件完整路徑:', new_file)# 復制多個文件到其他目錄并且重命名:方法二 def rename_files(file_path,type,new_path):if not os.path.exists(new_path):os.makedirs(new_path)files = get_files(file_path, type)for i in range(len(files)):file = files[i]new_file = os.path.join(new_path, '%03d_'%(len(files)-i) + os.path.basename(file))byte_file = get_file(file) # 二進制方式打開save_file(new_file,byte_file) # 二進制方式存儲print('重命名前的文件為:%s,重命名后文件為:%s' %(file,new_file))

?

總結

以上是生活随笔為你收集整理的【Python】shutil内置模块复制和重命名文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。