#欢迎交流:shuilinggan@163.com#
工作中,经常会遇到需要修改或新建很多文件的文件命,是一件重复且头疼的事情,下面一组Python代码,轻松教你快速实现!
程序一:根据文件(SSSSSS)新建并命名空文件夹
import shutil,osimport xlrdimport reimport xlwt#创建并命名空文件夹
data1 = xlrd.open_workbook('./SSSSSSS.xlsx')
table1 = data1.sheets()[0]
nrows1 = table1.nrows
ncols1 = table1.ncols
for i in range(1,nrows1):
# a = str(table1.row(i)[1].value)
# code = int(a[:9])
# print(code)
name = table1.row(i)[0].value
print(name)
os.mkdir("./GGGGGGG/"+name)
程序二:将各类文件按要求(统一格式)重新命名重命名文件
root = (r"C:\Users\Desktop\玲\重命名文件\XXXXXX")
for rt,dirs,files in os.walk(root):
for file in files:
print(file)
shutil.move(root+"\\"+file,root+"\\YYYYYYYYYY"+file)
程序三:将Excel文件中以文件夹名称相同的数据拉出来独立形成一个Excel文件并放入对应文件夹中,同时统计各文件夹中文件的数量
import xlrd
import re
import xlwtdata1 = xlrd.open_workbook('./SSSSSS.xlsx')
data2 = xlrd.open_workbook('./XXXXXX.xlsx')
table1 = data1.sheets()[0]
table2 = data2.sheets()[0]
# table.row_values(1)
nrows1 = table1.nrows
ncols1 = table1.ncols
nrows2= table2.nrows
ncols2= table2.ncols
print('nrows1:',nrows1)
print('总列数:',ncols2)
for i in range(2,nrows1):
sum = 0
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = book.add_sheet('sheet1', cell_overwrite_ok=True)
a = str(table1.row(i)[0].value)
col_A = int(a[:9])
col_B = table1.row(i)[1].value
title = table2.row_values(0)
for m in range(0, len(title)):
sheet.write(0, m, title[m])
for j in range(1,nrows2):
b = str(table2.row(j)[15].value)
col_P = int(b[:9])
# print(col_P)
if (col_A == col_P):
sum=sum+1
data = table2.row_values(j)
# print('data',data)for k in range(0,len(data)):
sheet.write(sum, k, data[k])
book.save('./599+662/'+ col_B + '.xls')
print(col_B, sum)
----End-----
扫码关注,惊喜多多