Search code examples
pythonmacosunixfindls

Theoretically simple: CSV of filenames and dates from all subdirectories (Unix(OSX)


so what I want to do seems simple but... I just can't get it to work.

I have a random folder with multiple subdirectories including a lot of files (like 2k). Now I want to write a little program / script to generate me an txt / csv file that ONLY contains: - filenames - last modified date and NOT: - full path to file - hidden files - directories

It is supposed to work under OSX. Tool of choice for me to give it an simple GUI is currently python. Basic idea: get a terminal command to work and use subprocess / os import to use that command from within the finished program with GUI.

While that works...I just seem to can't get an appropriate terminal command working.

Any help highly appreciated, thanks in advance!

I tried native python solutions without calling os; but apparently my python skills simple aren't good enough for that.

I tries building pipes with ls, find, awk, sed, mtime, stat, ...basically throwing anything together coming to mind but..somehow I just mess it up.

All solutions I can't find online target slightly different problems and I am not able to put all the pieces together in one big pipe apparently. One piece I add seems to always break another.

find PATH -type f -execdir echo {} ';' > output.txt

Gets me all filenames without directories, hidden files, or full path. This is quite what I want, now I just need a second column in the output with last modified dates...

ls -lRT | awk '{print "\""substr($0,index($0,$10))"\""",""\""$6" "$7", "$9"\""",""\""$5"\""}' > list.csv

Almost there but...besides Excel messing up the CSV, I can't get rid of the full path, but I only need names and dates...

find PATH -type f | awk '{print "\""substr($0,index($0,$10))"\""",""\""$6" "$7", "$9"\""",""\""$5"\""}' > folderlist.csv

So and well...this shows that minus times minus doesn't always equal plus. Won't get where I want on this one as well.


Solution

  • import os
    import time
    import csv
    
    
    PATH = '.'  # This is your start folder, taking current folder as an example
    csv_f = list()
    for root, dirs, files in os.walk(PATH):
        for file in files:
            if not file.startswith('.'):
                filepath = os.path.join(root, file)
                if os.path.isfile(filepath):  # This check excludes symlinks
                    m_time_raw = os.path.getmtime(filepath)
                    m_time = time.ctime(m_time_raw)
                    csv_f.append([file, m_time])
    
    with open('temp.csv', 'w', newline='') as c:
        writer = csv.writer(c)
        writer.writerows(csv_f)
    c.close()