python批量复制并重命名文件
答案:1 悬赏:0 手机版
解决时间 2021-02-06 22:42
- 提问者网友:沉默菋噵
- 2021-02-06 03:22
假设有一张图片,准备将其拷贝成多张(TXT里有多少个名字就将其拷贝成多少张)并对其命名,名字从一个TXT文本里读取(或者Excel表)。
最佳答案
- 五星知识达人网友:洎扰庸人
- 2021-02-06 03:49
#! /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.')
# 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.')
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯