程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Copy to a new folder and rename it conditionally in Python multiple folders

編輯:Python

Put the folder 2010,2011,label Documents in , Copied to the res/train1 … In the document ,train1 Is a new folder , And rename

import shutil
import os
def read_file_path(path):
# Read file name
return [i for i in os.listdir(path)]
def mkdir(path):
# Create folder
import os
path = path.strip()
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
# print (path+' Create success ')
return True
else:
# print(path+' directory already exists ')
return False
def copy_file_rename(old_path, new_path):
# Copy files to the specified address
try:
shutil.copy(old_path, new_path)
except Exception as e:
print(e)
def res(path_list):
label = 0
for i in path_list:
file_list = read_file_path(i)
for j in file_list:
old_path = f"{i}/{j}"
mkdir(r'res/train{}'.format(j.split(".")[0]))
if label == 0:
new_path = r'res/train{}'.format(j.split(".")[0]) + "/t0.tif"
elif label == 1:
new_path = r'res/train{}'.format(j.split(".")[0]) + "/t1.tif"
else:
new_path = r'res/train{}'.format(j.split(".")[0]) + "/label.png"
copy_file_rename(old_path, new_path)
label += 1
# Don't put it in the wrong order , Otherwise, something will go wrong
res(["2010", "2011", "label"])

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved