python学习随笔

打开文件管理器窗口方式

方式一

import subprocess #执行指令,explorer是windows文件管理器的指令
目录
subprocess.Popen(r’explorer “C:\Users\dell\Desktop\hmui”')

方式二

os.startfile(‘C:\Users\dell\Desktop\hmui’)# 打开文件夹窗口
os.startfile(‘C:\Users\dell\Desktop\hmui\hmui.py’)# 打开文件(不止txt文件哦,可以打开所有文件)

打开文件浏览器并选择文件路径

pip install pywin32
python的windows接口封装,在c:\users\dell\appdata\local\programs\python\python36\lib\site-packages
路径下有相应的pywin32接口说明chm文件。

使用win32ui打开文件管理器并选择文件

  import win32ui # 引入模块
  dlg = win32ui.CreateFileDialog(1) # 参数 1 表示打开文件对话框
  dlg.SetOFNInitialDir('xxxxxxxxx') # 设置打开文件对话框中的初始显示目录
  dlg.DoModal()
  filename = dlg.GetPathName()
  filepath = dlg.GetPathNames()

使用win32com.shell打开文件管理器并选择文件夹

  from win32com.shell import shell, shellcon

  desktop_pidl = shell.SHGetFolderLocation(0, shellcon.CSIDL_DESKTOP, 0, 0)
  pidl, display_name, image_list = shell.SHBrowseForFolder(
      win32gui.GetDesktopWindow(),
      desktop_pidl,
      "Choose a folder",
      0,
      None,
      None
  )
  # print(shell.SHGetPathFromIDList(pidl))

sys.path 和 os.path区别:

sys主要是系统环境,sys.path.insert, sys.path.append ,把路径加入到当前环境
os主要是操作系统文件 os.path.jion拼接路径

查找模块安装路径

pip show pywin32

str 和 bytes的转换

有byte类型和str,byte(‘abc’, ‘ascii’), str(b’abc’, encoding=“utf-8”),
bytes.decode(b’abc’)
或者b’abc’.decode(encoding=‘utf-8’), ‘abc’.encode(encoding=‘utf-8’)
比较乱,我也不晓得

  b1 = b'Hello'
  s1 = 'Hello'
  print(type(b1))
  print(type(s1))
  # bytes类型转换为str类型
  # 方法1 str()函数
  s2 = str(b1, encoding="utf-8")
  print(s2)
  print(type(s2))
  # 方法2 bytes.decode()函数
  s3 = bytes.decode(b1)
  print(s3)
  print(type(s3))

抛出异常

try:
shutil.copytree(originPath, newProjectPath)
except :
return False, ‘工程已存在’

字符串格式化

age = ‘hello’
name = ‘world’

1.直接拼接
info = age + name
2.%
info = ‘this is %s %s’ % (age, name)
3.format
info = ‘this is {_age} {_name}’.format(_age=age, _name=name)
info = ‘this is {0} {1}’.format(age, name)

判断后缀名

第一种:splittext()方法
os.path.splittext(path)[-1]

第二种:endswith()方法
path = “test_user_info.py”
bool = path.endswith(“.py”)

第三种:pathlib方法
import pathlib
pathlib.Path(“/Users/pankaj/abc.txt”).suffix

定义结构体

class allWidget:
def init(self):
self.xlxs_path = None
self.sheet_num = None
self.en_column = None
allWidgets = allWidget()

启用线程

import threading

def listen_music(num):
print(“----> %d” % num)

t1 = threading.Thread(target=listen_music, args=(13,))

转换大小写

upper():所有字母大写
lower():所有字母小写
capitalize():首字母大写,其他字母小写
title():每个单词首字母大写,其他小写

数据类型转换

int(x [,base]) ⇒ 将x转换为一个十进制的整数 , base:应该是进制,默认十进制
long(x [,base]) ⇒ 将x转换为一个十进制的长整数
float(x) ⇒ 将x转换为一个浮点数
str(object) ⇒ 转换为字符串
repr(object) ⇒ 转换为表达式字符串
eval(str) ⇒ 用来计算在字符串中的有效Python表达式,并返回一个对象
tuple(seq) ⇒ 将序列seq转换为一个元组
list(seq) ⇒ 将序列seq转换为一个列表
chr(x ) ⇒ 将一个整数转换为一个字符
unichr(x ) ⇒ 将一个整数转换为Unicode字符
ord(x ) ⇒ 将一个字符转换为它的整数值
hex(x ) ⇒ 将一个整数转换为一个十六进制字符串
oct(x ) ⇒ 将一个整数转换为一个八进制字符串

读写xml的库

lxml, xml.etree.ElementTree

打包python为exe

安装pyinstaller工具
pip install pyinstaller

执行命令:
执行 pyi-makespec -F xx.py, 可以生成xx.spec文件
修改spec文件
# -*- mode: python ; coding: utf-8 -*- block_cipher = None a = Analysis(['./demos/ui.py', './demos/ui_genc.py', './demos/ui_trans.py', './src/python/awtk.py'], pathex=['E:\\0-project_code\\15-toolslib\\6-awtk-script\\awtk-hmui'], binaries=[], datas=[], hiddenimports=[], hookspath=[], runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher, noarchive=False) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, [], name='HmUiComplex', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, upx_exclude=[], runtime_tmpdir=None, console=False, icon='bitbug.ico')
执行 pyinstaller -F xx.spec打包生成exe

pyinstaller -F -i bitbug.ico xx.py
-i bitbug.ico为生成的exe图标;
-F 只生成一个exe文件
pyinstaller -F -w -i 图标.ico *.py(支持多个) -F:只打包为一个exe,-w:去掉cmd窗口,-i:添加图标

界面设计工具:pyqt5,pyuic5
pyuic5 -o *.py *.ui (ui文件为xml文件,其实就是qt designer设计的结果)

将exe和资源文件打包为steup安装包

参考“inno使用说明.docx”文档

系统命令执行

subprocess模块,os.system os.popen

全局变量

变量前加global

python读取ini文件

安装configparser包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值