Search code examples
pythoncsvstandard-library

python code doesn't generate the csv file


AIM: I wanted to record all of the files on a variety of hard disk and collects the: file name, folders, and size of the file in megabytes. The code runs, to my knowledge doesn't produce any errors, but doesn't produce the csv file at the end?

What I've tried: I've tried running the file with Sudo, changing the permissions with chmod +x, checking that python is in the same place for standard user and for sudo user, and lastly removing or commenting out troublesome lines which seem to yield different results or errors depending on OS.

import os
from os import path
import sys
import datetime
from datetime import date, time, timedelta
import time
import csv
#from socket import gethostbyname

#variables
#hostname = str(socket.gethostname())
scandir = "/"
savefiledir = "/Users/joshua/Documents/python/"
textfilename = str(datetime.datetime.now().strftime("%Y-%m-%d")) + "_" "directory_printer.csv"

#change directory to the root directory or the one which you want to scan for files (scandir)
os.getcwd()
os.chdir(scandir)
directory = os.getcwd()

#find all files in a directory and it's sub directory regardless of extension
results = [val for sublist in [[os.path.join(i[0], j) for j in i[2]] for i in os.walk(directory)] for val in sublist]

d = {}
file_count = 0
metadata = []

for file in results:
    #full path
    try:
        fullpath = file
    except:
        fullpath = None

    #file name
    try:
        file_directory = "/".join(str(file).split('/')[1:-1])
    except:
        file_directory = None

    #file extension
    try:
        file_ext = str(file).split('/')[-1]
    except:
        file_ext = None

    #subfolders
    try:
        parts = file_directory.split('/')
        sub_folders = ":".join(parts[1:-1])
    except:
        sub_folders = None

    #num subfolders
    try:
        count_subfolders = len(sub_folders.split(':'))
    except:
        count_subfolders = None

    #filesize megabytes
    try:
        filesize_mb = os.path.getsize(file)/1024
    except:
        filesize_mb = None

    #date modified
    try:
        date_modified = datetime.datetime.now() - datetime.datetime.fromtimestamp(path.getmtime(file))
    except:
        date_modified = None

    #time modified
    #try:
#        time_modified = os.stat(fullpath).st_mtime #time of most recent content modification
    #except:
#        time_modified = None


    #time created (windows)
#    try:
#        time_created = os.stat(fullpath).st_ctime #platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows)#    except:
#        time_created = None

    #record all file metadata
    d[file_count] = {'Full_Path': fullpath, 'File_Directory': file_directory,
                     'File_Extension': file_ext, 'List_Sub_Folders' : sub_folders,
                    'Count_Sub_Folders' : count_subfolders, 'Filesize_mb' : filesize_mb,
                     'Date_Modified' : date_modified}

    file_count = file_count + 1

#write the dictinary with the disks file metadata to a csv file
with open(textfilename,'w') as f:
    w = csv.writer(f)
    w.writerows(d.items())

print("Scanning directory: "
      + str(scandir) + " complete!" + "\n"
      + "The results have been saved to: " + "\n"
      + str(savefiledir)+str(textfilename))

Solution

  • As it is, it looks like your code will write the CSV file to scandir (/), not to savefiledir, because at the beginning of the program you call os.chdir(scandir). If you want to get the file at the right place (where the final printed message says it's saved to) you should do:

    # ...
    #write the dictinary with the disks file metadata to a csv file
    with open(savefiledir + textfilename,'w') as f:
        w = csv.writer(f)
        w.writerows(d.items())
    # ...