0

I am currently writing a script that generates a report (output is .csv) on directory contents. Each report is unique in that it saves with unique date/timestamp, so the report doesn't save over itself or append to the same file each time.

The column headers in the report are as follows;

header = ['File_Pathway', 'Subdir', 'File_Name', 'Extension', 'Size_(in_bytes)', 'File_Created', 'Last_File_Save_Date', 'File_Author', 'Last_Saved_By_User_X']

I am struggling to get the File_Author and Last_Saved_By_User_X, but found a script here that collects this information using file metadata:

import win32com.client
sh=win32com.client.gencache.EnsureDispatch('Shell.Application',0)
ns = sh.NameSpace(r'm:\music\Aerosmith\Classics Live!')
colnum = 0
columns = []
while True:
    colname=ns.GetDetailsOf(None, colnum)
    if not colname:
        break
    columns.append(colname)
    colnum += 1

for item in ns.Items():
    print (item.Path)
    for colnum in range(len(columns)):
        colval=ns.GetDetailsOf(item, colnum)
        if colval:
            print('\t', columns[colnum], colval)

The issue I run into is with ns = sh.NameSpace(r'm:\music\Aerosmith\Classics Live!') as it only takes raw strings. The pathway that I want to pass to sh.NameSpace is a variable that loops through the directory, it's the current_filepath as the script is looping through the directory of files.

I have tried every method from this article to convert the string variable into a raw string to pass through this function but nothing is working. Can anyone help shed some light on this for me?

For more context, here is some more sample code from the script I am writing to show you what the current_filepath variable is:

    rootdir = input('Enter directory pathway: ')
    count = 0
    
    datetime_for_filename = datetime.now()
    datetime_for_filename_format = str(datetime.strftime(datetime_for_filename, '%Y-%m-%d--%H-%M-%S'))
     
    filename_with_datetimestamp = 'filename_printout' + '-' + datetime_for_filename_format + '.csv'
    
    header = ['File_Pathway', 'Subdir', 'File_Name', 'Extension', 'Size_(in_bytes)', 'File_Created', 'Last_File_Save_Date', 'File_Author', 'Last_Saved_By_User_X']
        

        for subdir, dirs, files in os.walk(rootdir):

              with open(filename_with_datetimestamp, 'a', newline='') as f:
                   writer = csv.writer(f)
                   current_subdir = subdir
                   try:
                       for filenames in files:
                       data_list = []
                       current_filepath = subdir +  '\\''' + filenames

                       raw_current_filepath = fr"{current_filepath}"
Markus
  • 5,976
  • 5
  • 6
  • 21
  • 1
    What errors are you getting? raw strings are just a different way of *defining* a string - once created, they are treated exactly like any other standard string. – matszwecja Aug 17 '22 at 14:04
  • Hi, please try to reduce it to a minimal working example. Define simple fake inputs and call the method. There is currently a ton of code and it is unclear what are you asking and what are the errors. Maybe you will even spot the problem yourself after you have the minimal example (e.g. 3 simple lines of code) ;-) – Filip Happy Aug 17 '22 at 15:01
  • I don't get an 'error' persay - the script still runs, but when I print(ns), it just prints 'None' type when I pass my current_filepath variable through sh.NameSpace(current_filepath). When I pass sh.NameSpace(r'C:\Users\Kjarzeck\Documents\python_code\time_study_program') and then have print(ns), it starts to print all the metadata associated with the file being passed. Hope that makes sense. – Katherine Jarzecki Aug 17 '22 at 16:21

0 Answers0