Python Forum
Issues with running regedit command from python
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with running regedit command from python
#1
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
Reply
#2
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.
Reply
#3
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?
Reply
#4
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/
Reply
#5
Thanks!
Reply
#6
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.
Reply
#7
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.
Reply
#8
I'm using the 'regobj' module as suggested by Larz60+

The Python version I'm running is 3.6.1
Reply
#9
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.
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked Issues Installing Pyenv/Python 3.9.1 Brandon_Contactum 1 2,461 Feb-22-2022, 06:32 PM
Last Post: snippsat
  Sound Approach for Running a Shell Command? matt_the_hall 8 3,254 Dec-14-2020, 02:52 PM
Last Post: matt_the_hall
  Running Python in Command Prompt Anwar 3 3,002 Nov-15-2020, 03:15 PM
Last Post: snippsat
  Error when running mktorrent subprocess command pythonnewbie138 4 3,761 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  Running linux command line apps... dbrdh 0 1,619 Jan-30-2020, 01:14 PM
Last Post: dbrdh
  TurtleWorld on MAC in Python 3.6 issues benniehanas 3 3,677 Jul-09-2018, 08:00 PM
Last Post: ichabod801
  Issues with Python script as service satishgunjal 8 7,364 Jun-21-2018, 11:40 AM
Last Post: satishgunjal
  python variable issues - using spyder and opencv Afrodizzyjack 5 5,711 Jun-19-2018, 09:46 AM
Last Post: gontajones
  Pip command issues in windows golmolbandar 1 2,896 Jun-12-2018, 07:58 PM
Last Post: snippsat
  issues while using Psexec from python code dev_devil_1983 0 3,502 May-28-2018, 07:55 AM
Last Post: dev_devil_1983

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020