Python Forum
Issues with running regedit command from python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Issues with running regedit command from python (/thread-3216.html)

Pages: 1 2


Issues with running regedit command from python - gohanzdad - May-06-2017

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



RE: Issues with running regedit command from python - Larz60+ - May-06-2017

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.


RE: Issues with running regedit command from python - gohanzdad - May-06-2017

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?


RE: Issues with running regedit command from python - Larz60+ - May-06-2017

Here's one example: https://github.com/jeffbryner/pyioc/blob/6310f9f5fcf6aa0e20767b30e3a084dbe6b9cd32/client/iocItems/RegistryItem.py
There is also a use in this package: https://pypi.python.org/packages/source/p/pythonselect/pythonselect-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/


RE: Issues with running regedit command from python - gohanzdad - May-06-2017

Thanks!


RE: Issues with running regedit command from python - gohanzdad - May-08-2017

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.


RE: Issues with running regedit command from python - snippsat - May-08-2017

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.



RE: Issues with running regedit command from python - gohanzdad - May-08-2017

I'm using the 'regobj' module as suggested by Larz60+

The Python version I'm running is 3.6.1


RE: Issues with running regedit command from python - snippsat - May-08-2017

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.


RE: Issues with running regedit command from python - gohanzdad - May-08-2017

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.