python - % : 'int' and 'str' 不支持的操作数类型

标签 python python-2.7 registry

我正在编写一个脚本,该脚本将使用 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'

最佳答案

os.system() 返回一个整数,您正在尝试将字符串插值应用到 that:

os.system('...') % regbackupfile

将字符串插值选项移动到字符串参数,然后再将其传递给 os.system():

os.system('...' % regbackupfile)

您可能应该看看使用 subprocess module相反,它允许您在不进行插值的情况下传递参数。

关于python - % : 'int' and 'str' 不支持的操作数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27722963/

相关文章:

python - 当前方法和类名进入日志记录;这些方法在功能上是否等效?

python-2.7 - Windows 上的 Theano + Cuda 8.0 - 编译错误

c# - 在 C# 应用程序中写入注册表

python - 为什么我创建的函数给我的是 None 类型?

Python类调用其他方法

python - 减少列值之间的数据框

python - 权限错误 : [Errno 13] Permission denied Python

python - 如何抓取这个 squawka 页面?

windows - Puppet:使用除非选项的条件执行

python - 如何在 python 3.4 中读取 winreg 中的 reg_qword ?