本文目錄一覽:
- 1、python批量複製並重命名文件
- 2、如何批量重命名,將一個文件夾內的文件依次重命名為另一個的
- 3、Python如何實現將一個文件夾裡面的文件重命名並放到另外一個文件夾裡面?
- 4、如何批量把多個文件夾內容合併到一個文件夾下,並自動重命名名稱相同的文件
python批量複製並重命名文件
#! /usr/bin/env python
# coding=utf-8
import os
import shutil
import time
import sys
reload(sys)
sys.setdefaultencoding(‘utf-8’)
def copy_and_rename(fpath_input, fpath_output):
for file in os.listdir(fpath_input):
#if os.path.splitext(file)[1] == “.jpg”:
oldname = os.path.join(fpath_input, file)
newname_1 = os.path.join(fpath_output,
os.path.splitext(file)[0] + “_1.jpg”)
newname_2 = os.path.join(fpath_output,
os.path.splitext(file)[0] + “_2.jpg”)
newname_3 = os.path.join(fpath_output,
os.path.splitext(file)[0] + “_3.jpg”)
#os.rename(oldname, newname)
shutil.copyfile(oldname, newname_1)
shutil.copyfile(oldname, newname_2)
shutil.copyfile(oldname, newname_3)
if __name__ == ‘__main__’:
print(‘start …’)
t1 = time.time() * 1000
#time.sleep(1) #1s
fpath_input = “C:/Users/jack/Desktop/shopimg/0708/”
fpath_output = “C:/Users/jack/Desktop/shopimg/0708/”
copy_and_rename(fpath_input, fpath_output)
t2 = time.time() * 1000
print(‘take time:’ + str(t2 – t1) + ‘ms’)
print(‘end.’)
如何批量重命名,將一個文件夾內的文件依次重命名為另一個的
不知道小夥伴們會不會遇到同時給多個文件修改名字的時候,那麼遇到了應該如何實現呢?可能大家第一反應就是快捷鍵F2,沒錯,這個鍵是可以幫助我們給多個文件批量重命名的,但是呢,命名還是有限制的。名字都是以(1)(2)(3)等帶括弧的方式,完全不能根據自己的需要設置。不過沒關係,今天小編推薦一個可以根據自己的喜好來修改文件名字,是可以批量重命名的哦,快來看看吧!
推薦使用:金舟批量重命名軟體
操作方法:
一、打開軟體後,點擊「添加文件」,然後將需要重命名的所有文件上傳到軟體中,可批量添加;
二、軟體支持以下三種方式重命名文件,分別是;自定義(直接修改整個文件名)、替換(替換某個字元)、插入(在原有的基礎下添加新的字元);
三、在右側可以看到三個區域,修改文件名、拓展名以及編號設置,根據自己的需要修改即可;
四、重命名文件後,在軟體中是可以實時預覽到的;
五、完成後,直接點擊「重命名」就可以了;
六、重命名成功後會得到以下提示!
Python如何實現將一個文件夾裡面的文件重命名並放到另外一個文件夾裡面?
import re
import os
def get_file_list(folder):
file_list = [];
for root, dirs, files in os.walk(folder):
for f in files:
path=root+os.path.sep+f
file_list.append(path)
return file_list
def get_re_file_list(file_list,re_rule):
file_list_re=[]
for file_path in file_list:
if re.search(re_rule,file_path):
file_list_re.append(file_path)
return file_list_re
def rename_basename(file_list_re,re_rule,new_str):
re_c = re.compile(re_rule)
new_file_list = []
for file_path in file_list_re:
old_base_name=os.path.basename(file_path)
new_base_name=re_c.sub(new_str,old_base_name)
new_full_path=os.path.join(os.path.dirname(file_path),new_base_name)
new_file_list.append (new_full_path)
return new_file_list
def rename_dir(root,new_root,file_list):
new_file_list=[]
re_c = re.compile(root)
for file_path in file_list:
new_file_path=re_c.sub(new_root,file_path)
new_file_list.append(new_file_path)
return new_file_list
def rename2list(old_list,new_list):
for i in range(0,len(old_list)):
os.rename(old_list[i],new_list[i])
def main():
root=r”/home/user1/exp1″
old_dir=”exp1″
new_dir=r”exp2″
re_rule=”xy_|_nk”
new_str=””
old_file_list=get_file_list(root)
re_file_list=get_re_file_list(old_file_list,re_rule)
new_basename_list=rename_basename(re_file_list,re_rule,new_str)
new_dir_list=rename_dir(old_dir,new_dir,new_basename_list)
rename2list(re_file_list,new_dir_list)
if __name__ == ‘__main__’:
main()
如何批量把多個文件夾內容合併到一個文件夾下,並自動重命名名稱相同的文件
1、首先將想要合併的文件夾裝到一個文件夾中。然後在該文件夾中新建一個文件夾和txt文件,均命名為all。
2、然後雙擊打開文本文檔,在編輯頁面中鍵入下方的代碼:
for /f “delims=” %%p in (‘dir /b/ad’) do copy %%p\*.* d:\txt\all\
pause,然後保存該文件。
3、然後右鍵單擊文本文檔,選擇重命名,輸入「all.bat」,回車確定,在彈出來的窗口中點擊選擇「是」、
4、之後滑鼠左鍵雙擊all.bat文件,界面顯示bat文件執行結果,均提示複製文件成功,點擊任意鍵後關閉該窗口。
5、進入all文件夾中查看,就可以看到已經全部合併到該文件夾中了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/184525.html