'''
Author: yanghaihong yanghaihong@feicun.com
Date: 2023-01-04 17:26:09
LastEditors: yanghaihong yanghaihong@feicun.com
LastEditTime: 2023-01-05 15:33:24
FilePath: /Python-excel-master/readExcel.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import xlrd
import xlwt
import os
import shutil
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@Author :xxxxx
@Contact :1223242863@qq.com
@File :zip.py
@Time :2021/8/17 11:27 PM
@Software :Pycharm
@Copyright (c) 2021,All Rights Reserved.
"""
import os
import zipfile
from loguru import logger
def getZipDir(dirpath, outFullName):
"""
压缩指定文件夹
:param dirpath: 目标文件夹路径
:param outFullName: 压缩文件保存路径+xxxx.zip
:return: 无
"""
zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_BZIP2)
for path, dirnames, filenames in os.walk(dirpath):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(dirpath, '')
for filename in filenames:
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()
logger.info("文件夹\"{0}\"已压缩为\"{1}\".".format(dirpath, outFullName))
# if __name__ == "__main__":
# getZipDir(dirpath=r"C:\Users\XZD\Documents\录音",
# outFullName=r"C:\Users\XZD\Documents\录音.zip")
import os
def mkdir(path):
folder = os.path.exists(path)
if not folder: #判断是否存在文件夹如果不存在则创建为文件夹
os.makedirs(path) #makedirs 创建文件时如果路径不存在会创建这个路径
print ("--- new folder... ---")
print ("--- OK ---")
else:
print ("--- There is this folder! ---")
# file = "G:\\xxoo\\test"
# mkdir(file) #调用函数
image_path = r'Y:\共享文件夹\_模型素材库\素材库'
# 遍历文件夹及其子文件夹中的文件,并存储在一个列表中
# 输入文件夹路径、空文件列表[]
# 返回 文件列表Filelist,包含文件名(完整路径)
def get_filelist(dir, Filelist, targetModelName):
newDir = dir
if os.path.isfile(dir):
dir = dir.lower()
# ttmp_idx2 = dir.find("fbx\\")
# ttmp_idx1 = dir.find(".fbx")
# ttmp_idx = dir.find("\\" + targetModelName + "\\")
# if ttmp_idx > -1 and ttmp_idx2 == -1 and ttmp_idx1 > -1:
# Filelist.append(dir)
# # 若只是要返回文件文,使用这个
# Filelist.append(os.path.basename(dir))
elif os.path.isdir(dir):
dir = dir.lower()
ttmp_idx1 = dir.find(".mayaswatches")
ttmp_idx2 = dir.find("fbx")
if ttmp_idx1 == -1 and ttmp_idx2 > -1 :
print("dir " + dir)
widx = dir.find("共享文件夹")
newstr = dir[widx:len(dir)]
targetZip = "E:\\" + newstr
if not os.path.exists(targetZip):
mkdir(targetZip)
getZipDir(dir,targetZip + ".zip")
for s in os.listdir(dir):
# 如果需要忽略某些文件夹,使用以下代码
#if s == "xxx":
#continue
newDir=os.path.join(dir,s)
get_filelist(newDir, Filelist, targetModelName)
return Filelist
xlsx_path = r'C:\Users\XZD\Documents\Tencent Files\2893764123\FileRecv\model.xlsx'
file_path = r'E:\models'
list = get_filelist(image_path, [],"")
# # 使用xlrd模块的open_workbook函数打开指定Excel文件并获得Book对象(工作簿)
# wb = xlrd.open_workbook(xlsx_path)
# # 通过Book对象的sheet_names方法可以获取所有表单名称
# sheetnames = wb.sheet_names()
# print(sheetnames)
# # 通过指定的表单名称获取Sheet对象(工作表)
# sheet = wb.sheet_by_name(sheetnames[0])
# # 通过Sheet对象的nrows和ncols属性获取表单的行数和列数
# # print(sheet.nrows, sheet.ncols)
# for row in range(sheet.nrows):
# for col in range(sheet.ncols):
# # 通过Sheet对象的cell方法获取指定Cell对象(单元格)
# # 通过Cell对象的value属性获取单元格中的值
# if row <= start_idx :
# continue
# value = sheet.cell(row, col).value
# if col == 3 and row > 0:
# value1 = sheet.cell(row, 0).value
# id_model = int(value1)
# print(id_model)
# list = get_filelist(image_path, [],value)
# # for file in list:
# # print("find: " + file)
# # source = file
# # target = file_path + "\\" + str(id_model) + ".fbx"
# # try:
# # shutil.copy(source, target)
# # except IOError as e:
# # print("Unable to copy file. %s" % e)
# # except:
# # print("Unexpected error:", sys.exc_info())
# # # 下载 重命名
# print()
# 将学生姓名和考试成绩写入单元格
# for row in range(len(scores)):
# sheet.write(row + 1, 0, student_names[row])
# for col in range(len(scores[row])):
# sheet.write(row + 1, col + 1, scores[row][col])
# # 保存Excel工作簿
# wb.save('考试成绩表.xlsx')
# 获取最后一个单元格的数据类型
# 0 - 空值,1 - 字符串,2 - 数字,3 - 日期,4 - 布尔,5 - 错误
# last_cell_type = sheet.cell_type(sheet.nrows - 1, sheet.ncols - 1)
# print(last_cell_type)
# 获取第一行的值(列表)
# print(sheet.row_values(0))
# 获取指定行指定列范围的数据(列表)
# 第一个参数代表行索引,第二个和第三个参数代表列的开始(含)和结束(不含)索引
# print(sheet.row_slice(3, 0, 5))
分享一下python遍历文件夹,读写Excel,压缩文件,上传百度云等操作
需积分: 5 118 浏览量
2023-02-22
17:50:52
上传
评论 1
收藏 16KB ZIP 举报
watl0
- 粉丝: 97
- 资源: 10
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈