主要步骤
- 获取开机图片所在的文件夹路径
- 将该文件夹下的所有文件复制到桌面名为“开机图片”的文件夹(如果没有则自动创建)
- 修改所有文件的文件扩展名为.jpg
- 使用pyinstaller将python文件打包成可执行文件
获取开机图片所在的文件夹路径
上一篇里我们提到,图片所在的文件路径如下:
C:\Users\username\appdata\Local\Packages\Microsoft.Windows.ContentDeliveryManager_********\LocalState\Assets\
这就要求我们首先获取WIN10当前用户名:
import getpass
userName=getpass.getuser()
然后进行路径拼接到Pachages文件夹下:
userName=getpass.getuser()
path=r"C:/Users/";
path=path+str(userName)+"/appdata/Local/Packages/"
在Packages文件夹下需要找到形如:Microsoft.Windows.ContentDeliveryManager_********
的文件夹。
那么首先我们需要获取Packages文件夹所有文件名(文件夹):
dirList=os.listdir(path)
# 返回的是一个list,该list的一项代表一个文件名
接下来需要用到正则表达式来匹配目标文件名了:
reMatch=re.compile(r'.*Microsoft.Windows.ContentDeliveryManager_.*')
.*
的意思是可以匹配任意多任意类型的字符。这个正则表达式的意思是,文件名前面是啥我不管,文件名后面是啥我不管,但是中间一定要有Microsoft.Windows.ContentDeliveryManager_
整体代码如下:
dirList=os.listdir(path)
reMatch=re.compile(r'.*Microsoft.Windows.ContentDeliveryManager_.*')
for item in dirList:
if(reMatch.match(str(item))):
path+=str(item)
最后,再进行字符串的拼接,获得最终的目标路径:
path+="/LocalState/Assets/"
将该文件夹下的所有文件复制到桌面名为“开机图片”的文件夹并修改扩展名
destiPath="C:/Users/"+userName+"/Desktop/开机图片"
# 如果桌面不存在“开机图片”这个文件夹,则创建
if(not os.path.exists(destiPath)):
os.mkdir(destiPath)
for fileItem in dirList:
filePath=os.path.join(path,fileItem)
fileName,fileExtend=os.path.split(fileItem)
shutil.copy(filePath,destiPath+"/"+str(fileExtend)+".jpg")
这里面需要注意的是,如果一个文件没有格式,那么在进行os.path.solit的时候,fileName为空,fileExtend为该文件的文件名。所以在copy时候用的是str(fileExtend)
使用pyinstaller生成可执行文件
参考这篇文章:Python脚本生成可执行文件
如果出现报错,参考这篇文章:pyinstaller错误与解决办法

网友评论