python必备代码_python-常用代码

1、遍历文件夹、子目录

import os

#提取文件目录,存成列表list_directory(列表有顺序,字典无序)

def get_directory(path,li=[]):

li.append(path)

"""

1、topdown=true从上往下遍历,false从底层往上遍历

2、dirs是walk遍历出来的,还需要再遍历一次。

3、files与dirs类似,也需要遍历2遍。

4、root是目录名绝对路径,dirs是单个目录名,files是单个文件名

"""

for root, dirs, files in os.walk(path, topdown=True):

for name in dirs:

li.append(os.path.join(root, name)) #将指定目录下的所有目录都添加到列表li,dirs是个可迭代的包含目录名

return li

2、遍历文件夹下的文件

import os

for root, dirs, files in os.walk(path, topdown=True):

for name in files:

if name.endswith(".tif"): #判断文件类型

path_file = os.path.join(root, name) #root是文件名name对应的目录

tif.get_tif(path_file)

elif name.endswith(".pdf"):

path_file = os.path.join(root, name)

pdf.get_pdf(path_file)

#变量组成列表

dic = [path, pdf.count, round(pdf.size, 2), pdf.page,

tif.count, round(tif.size, 2),tif.page]

return dic

3、获取电脑桌面路径

import winreg

#获取桌面路径

def get_desktop():

key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,

r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',)

return winreg.QueryValueEx(key, "Desktop")[0]

4、数据写入excel

import xlwt

# 将数据写入excel

def data_write(file_path, datas):

#创建一个工作簿

f = xlwt.Workbook()

#创建sheet1

sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)

#插入表头

header = ['path','pdf_count','pdf_size','pdf_page','tif_count','tif_size','tif_page']

for k in range(len(header)):

sheet1.write(0,k, header[k])

# 将数据写入第 i 行,第 j 列

i = 1

for data in datas: #datas就是一个列表(行),里面存的还是列表(列)

for j in range(len(data)):

sheet1.write(i, j, data[j]) #data[j]是数据内容

i = i + 1

#获取桌面路径

file_path=get_desktop()

file_path=f'{file_path}/data.xls'

#保存excel文件,需要带上写入的文件名称

f.save(file_path) #f是上面存的工作簿对象

#打开文件

os.system(f"start {file_path}")

5、pdf文件页数的几种方法

import pdfplumber

def get_pdf_pages(path):

f = pdfplumber.open(path)

return len((f.pages))

from PyPDF2 import PdfFileReader

def get_num_pages(file_path):

"""

获取文件总页码

:param file_path: 文件路径

:return:

"""

reader = PdfFileReader(file_path)

# 不解密可能会报错:PyPDF2.utils.PdfReadError: File has not been decrypted

if reader.isEncrypted:

reader.decrypt('')

page_num = reader.getNumPages()

return page_num

from pdfrw import PdfReader

def get_pages(file_path):

x=PdfReader(path)

num_page0=len(x.pages)

returnnum_page0

6、判断字符串是否包含空格

import re #正则模块

#传入字符串,返回true或者false

def is_space(char):

"""判断是否包含空格"""

if re.search(r"\s",char):

return True

else:

return False

7、获取tif文件页数

from PIL import Image

# 获取文件累计页数

def get_page(self,path):

img = Image.open(path)

self.page=self.page+img.n_frames

8、多进程多线程的使用

from concurrent.futures import ProcessPoolExecutor,as_completed

import multiprocessing

if __name__ == '__main__':

#防止windows多进程循环调用

multiprocessing.freeze_support()

# future.map函数,也是异步处理的,map函数会根据li列表的顺序返回对应值。

# ProcessPoolExecutor() 不写参数代表根据电脑最大cpu核数取值,创建进程池

with ProcessPoolExecutor(max_workers=7) as executor:

for prime in executor.map(get_file, li): #遍历li列表,将遍历的值传给函数get_file执行

l_queue.append(prime)

#最新版本,使用as_completed处理异步

with ProcessPoolExecutor(max_workers=7) as executor:

futures = [executor.submit(get_file, item) for item in li] #根据列表li提交任务

for future in as_completed(futures): #当get_file返回时,再遍历取值,异步的

l_queue.append(future.result())

9、获取py文件路径

file_name=os.path.dirname(__file__)

10、弹窗获取用户选择的路径

import tkinter as tk

from tkinter import filedialog

root = tk.Tk()

root.withdraw()

file_path = filedialog.askdirectory() #用户选择的路径

11、打包应用程序

pyinstaller -F 打包成一个应用程序(不写打包成一个目录) -i 图标名称 start.py(打包的py主文件)

#终端切换到目录执行

D:\互联网项目\vba\datatools-副本>pyinstaller start.py

#指定-F参数容易失败,多进程要加multiprocessing.freeze_support()

12、类的写法

#基类

class Base:

# 获取文件大小、数量

def __init__(self):

self.count = 0

self.size = 0

self.page = 0

# 获取文件累计大小

def get_size(self,path):

fsize = os.path.getsize(path)

fsize = fsize / float(1024 * 1024)

self.size=fsize+self.size

#print(f'文件大小是{fsize}')

# 获取文件累计数量

def get_count(self):

self.count=self.count + 1

#子类

#tif类

class tif_info(Base):

def __init__(self):

super().__init__() #调用父类的init方法

# 获取文件累计页数

def get_page(self,path):

img = Image.open(path)

self.page=self.page+img.n_frames

# 封装调用3个类方法

def get_tif(self,path):

self.get_count()

self.get_size(path)

self.get_page(path)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值