Search code examples
pythonpython-2.7registry

Unsupported operand type(s) for %: 'int' and 'str'


I'm working on a script that will grab a Windows Registry export using os.system. For some reason, it seems to be having trouble with converting the file path to a string, but I might be mistaken.

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()

Error I get is:

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'

Solution

  • os.system() returns an integer, you are trying to apply the string interpolation to that:

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

    Move the string interpolation option to the string argument, before passing it to os.system():

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

    You probably should look at using the subprocess module instead, it lets you pass in arguments without interpolation.