Python Forum

Full Version: Adding regedit value to glob.glob
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do I add a value from regedit for example, location of a file to glob.glob function?

I wanted to use glob.glob to read a particular folder for files with a particular extension for example .exe. The particular folder to read must be the location stored in regedit. So, How do I get it done?

from winreg import *
import glob, os

key = OpenKey(HKEY_CURRENT_USER, r'SOFTWARE\Ubisoft', 0, KEY_ALL_ACCESS)
val = QueryValueEx(key, "ac_syndicate_exe")
print(val)

path = str(val)
for filename in glob.iglob('path/**/*.cs', recursive=True ):
   print(filename)
Also there's a small issue;

In regedit the path is stored with \\ whereas in normal drive path its stored with \ ...example:


E:\\Games (Stored)\\Ubisoft\\ac3.exe -> Path stored in Regedit
E:\Games (Stored)\Ubisoft\ac3.exe -> Drive Path
Here you go. I rewrote your code just a little bit. But it should do what you wanted now:

from winreg import *
import glob, os
 
key = OpenKey(HKEY_CURRENT_USER, r'SOFTWARE\Ubisoft', 0, KEY_ALL_ACCESS)
# Get the path and make it more workable. Note the escaped characters
path = QueryValueEx(key, "ac_syndicate_exe")[0].replace("\\\\", "/")

# Trim off the actual executable file name
path = path[0:path.rfind("/")]
print(path)

for filename in glob.iglob(path + "/path_to_files/*.cs", recursive=True):
    print(filename)
(Apr-25-2017, 08:34 AM)AlterBlitz Wrote: [ -> ]Also there's a small issue;

In regedit the path is stored with \\ whereas in normal drive path its stored with \
Are they? Or is that just what you see when you print the path? Backslashes are normally escaped when printing to the console, fyi:

>>> path = r"E:\some\dir\file.txt"
>>> path
'E:\\some\\dir\\file.txt'
>>> print(path)
E:\some\dir\file.txt