我正在编写一个脚本,该脚本将使用os.system获取Windows注册表导出。出于某种原因,将文件路径转换为字符串似乎有问题,但我可能会弄错。
from module_locator import module_path
import os
import datetime
myDate = datetime.datetime.now().strftime("%y-%m-%d")
myTime = datetime.datetime.now().strftime("%H:%M")
myDateTime = datetime.datetime.now().strftime("%y-%m-%d %H:%M")
scriptdir = module_path()
logdir = scriptdir + '\\logs\\'
tempdir = scriptdir + '\\temp\\'
regbackupfile = tempdir + "PlexRegistry-" + myDate + ".reg"
PlexDBDir = "C:\\Users\\Administrator\\AppData\\Local\\Plex Media Server"
def main():
#Setting up directories for logs and temp work
if not os.path.exists(logdir):
os.makedirs(logdir)
if not os.path.exists(tempdir):
os.makedirs(tempdir)
os.system('regedit /E %s "HKEY_CURRENT_USER\\Software\\Plex, Inc.\\Plex Media Server"') % regbackupfile
print "All Done. Check it out."
if __name__ == '__main__':
main()
我得到的错误是:
C:\Users\Administrator\Dropbox\Python Dev\Plex Backup>Plex_Backup.py
Traceback (most recent call last):
File "C:\Users\Administrator\Dropbox\Python Dev\Plex Backup\Plex_Backup.py", l
ine 87, in <module>
main()
File "C:\Users\Administrator\Dropbox\Python Dev\Plex Backup\Plex_Backup.py", l
ine 78, in main
os.system('regedit /E %s "HKEY_CURRENT_USER\\Software\\Plex, Inc.\\Plex Medi
a Server"') % regbackupfile
TypeError: unsupported operand type(s) for %: 'int' and 'unicode'
答案 0 :(得分:4)
os.system()
返回一个整数,您试图将字符串插值应用于 :
os.system('...') % regbackupfile
将字符串插值选项移动到字符串参数,然后将其传递给os.system()
:
os.system('...' % regbackupfile)
您可能应该考虑使用subprocess
module,它可以让您传入参数而无需插值。