日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

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

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

# -*- coding: utf-8 -*- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #作者:cacho_37967865 #博客:https://blog.csdn.net/sinat_37967865 #文件:rename_file.py #日期:2020-05-02 #備注: 批量對文件進(jìn)行重命名、批量復(fù)制文件 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''import os import shutil from pythonlession.basefunction.get_all_file import get_files from pythonlession.basefunction.deal_file import get_file,save_file# 復(fù)制某個文件: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('復(fù)制后的文件完整路徑:',new_file)return new_file# 復(fù)制某個文件并重命名: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('復(fù)制并重命名后的文件完整路徑:',new_file)return new_file# 復(fù)制某個文件生成多個重命名文件:sample-原始文件、new_path-新文件目錄、num-生成數(shù)量 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('復(fù)制并重命名后的文件完整路徑:',new_file)# 復(fù)制多個文件到其他目錄: 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('復(fù)制后的文件完整路徑:', new_file)# 復(fù)制多個文件到其他目錄并且重命名:方法一 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('復(fù)制并重命名后的文件完整路徑:', new_file)# 復(fù)制多個文件到其他目錄并且重命名:方法二 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) # 二進(jìn)制方式打開save_file(new_file,byte_file) # 二進(jìn)制方式存儲print('重命名前的文件為:%s,重命名后文件為:%s' %(file,new_file))

?

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。