Python Forum

Full Version: Issues with running regedit command from python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I've been working on a script for someone and they need the Windows registry backed up before the script is run.  I've been using the code below, but I recently noticed that it's not getting all of the registry backup.  I added the check=True argument to see if it would raise an error, but no error was raised.  When I run the command in the regular command prompt window, I end up with a 194MB file.  However, when I run the same command using the code below, I end up with a 117MB file.  Any suggestions on how the file could be cut short like that?

    def backup_registry(self) -> None:
        try:
            print("Backing up the registry.")
            cmd = "regedit.exe /a /e " + self.write_path + "EntireRegistry.reg"
            subprocess.run(cmd, check=True)
        except subprocess.CalledProcessError:
            raise
You might want to take a look at regobj so that you have more control over the process.
see: https://pypi.python.org/pypi/regobj/0.2.2
Using a command call could be a security issue.
Would it be possible for you to point me toward some examples of the code usage?  Maybe something that shows iterating over the entire registry?
Here's one example: https://github.com/jeffbryner/pyioc/blob...tryItem.py
There is also a use in this package: https://pypi.python.org/packages/source/...1.3.tar.gz module: pysel.py
I can't vouch for either of these, but can't find a lot either.

A good place to search for python examples for anything is http://nullege.com/
Thanks!
I've got this mostly working, however there are few registry values that are raising a 'UnicodeEncodeError' error. I'm pretty sure this is because of the binary and hex values. What is the best way to convert these to strings? The other values convert from bytes to string without any issues.
What are using and what is the code?
winreg Python 3 is what you should be using.

For Python 2 there where a winreg_unicode module.
Quote:The winreg_unicode package is a drop-in replacement for Python 2’s _winreg module.
However,it returns unicode values where possible, similar to Python 3’s winreg module.
I'm using the 'regobj' module as suggested by Larz60+

The Python version I'm running is 3.6.1
regobj there has been no update since 2011,
so i don't know if it handle Unicode as Python 3 do.
For Python 3.6 use winreg.
I looked at the code, and it does check for Python 3.  If it finds it then it uses winreg.  However, it could be missing something.
Pages: 1 2