我正在编写一个程序,用于更改我的桌面背景。它通过读取文本文件来实现。如果文本文件中写入了某个背景文件名,则将其保存为我的桌面背景,并将另一个文件名写入该文件并关闭它。
我无法让它正常工作。 以下是我的代码:
当我使用
我无法让它正常工作。 以下是我的代码:
import sys, os, ctypes
BGfile = open('C:\BG\BG.txt', 'r+' )
BGread = BGfile.read()
x=0
if BGread == 'mod_bg.bmp':
x = 'BGMATRIX.bmp'
BGfile.write('BGMATRIX.bmp')
BGfile.close()
elif BGread == 'BGMATRIX.bmp':
x = 'mod_bg.bmp'
BGfile.write('mod_bg.bmp')
BGfile.close()
pathToImg = x
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pathToImg, 0)
当我使用
"r+"
时,会出现以下错误:Traceback (most recent call last):
File "C:\BG\BG Switch.py", line 13, in <module>
BGfile.write('mod_bg.bmp')
IOError: [Errno 0] Error
这一点都不有用!
当我使用"w+"
时,它只会擦除文件中已经存在的内容。
有人可以告诉我为什么会出现这个奇怪的错误,并提供可能的解决方法吗?