0

I wrote the below code to be able to iterate through all files, sub-folders and folders given a file path. However, when I run the code it keeps giving me file not found error. It's also worth mentioning that when I run same code on my mac, it was giving me the same error, but I when I set follow_symlink=False the error disappeared.

Could anyone help me with what I'm doing wrong here?

[enter image description here][1]

from pathlib import Path
import csv
import os
import time
from datetime import datetime

class File:
    def __init__(self, folder_path):
        self.folder_path = Path(folder_path)
   
    def folders(self):
        return [folder for folder in self.folder_path.iterdir() if folder.is_dir()]
   
    def summary(self):
        errors = []
        data_table = []
        folders =  self.folders()
        for folder in folders:
            total=0
            count = 0
            files = folder.glob('**/*')
            for file in files :
                try:
                    total+=(os.stat(file, follow_symlinks=False).st_size)
                    count+=1
                except OSError as e:
                    errors.append(f'Error with file {file} in folder {folder} Error-->{e}')      
                    lm_date = datetime.strftime(datetime.strptime(time.ctime(os.stat(folder).st_mtime),
                                                                  '%a %b %d %H:%M:%S %Y'), '%d-%b-%Y')
                    data_table.append({'Path':folder, 'Size in MB':f'{total/1048576}', 'File Count':count,
                                       'Last Modifed Date':lm_date})
            lm_date = datetime.strftime(datetime.strptime(time.ctime(os.stat(folder).st_mtime),
                                                                  '%a %b %d %H:%M:%S %Y'), '%d-%b-%Y')
            data_table.append({'Path':folder, 'Size in MB':f'{total/1048576}', 'File Count':count,
                                       'Last Modifed Date':lm_date})
        return {'data':data_table, 'error':errors}  

    def csv_export(self, file_path):
        data_list =  self.summary()['data']
        headers = data_list[0].keys()
        with open(file_path, 'w') as fp:
             writer = csv.DictWriter(fp, headers)
             writer.writeheader()
             try:
                 for data in data_list:
                        writer.writerow(data)
                 print(f'file saved to location: {os.path.join(os.getcwd(), file_path)}')
             except OSError as e:
                print(f'Error exporting data file {e}')

[1]: https://i.stack.imgur.com/0YxQl.jpg Here's the error I'm getting.

pyzer
  • 122
  • 7
  • aalways put code, data and full error message as text (not screenshot, not link) in question (not in comment). – furas Jul 23 '21 at 13:37
  • Could it be that your path is too long? I believe there is a restriction on path length in Windows (256 characters) for some applications; in which case this is not a python but a Windows problem. – Romain Reboulleau Jul 23 '21 at 13:37
  • first get this path and check if it exists on disk. I'm not sure but other problem can be when it is disk mouted by network and Python has problem to work with this. – furas Jul 23 '21 at 13:40
  • If your path is indeed too long, then see [this post](https://stackoverflow.com/a/21194605/5386938) which suggests prefixing your path with `"\\?\"`, e.g., `"\\?\D:\very long path"` –  Jul 23 '21 at 14:01

2 Answers2

1

The old DOS/Win 'subst' command might help here. In this way you can replace a 'long base path' by a single 'drive letter' (plus double colon).

>help subst

>subst <drive2:> <drive1:>\<long_base_path>

But by the way: Why don't you use 'os.walk()'? This 'walks' through the whole given root directory:

import os

for dirName, subDirs, fileNames in os.walk('<root_dir>'):
  print('Current directory: ' + dirName)
  for dir in subDirs:
    print('Sub directory in ' + dirName + ': ' + dir)
  for fName in fileNames:
    print('File in ' + dirName + ': '+ fName)
Jacky
  • 11
  • 2
0

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string" where "" represents the invisible terminating null character for the current system codepage.

According to the Microsoft website Your path is too long, make sure it is less then 260 characters. Python's OS module can't deal with paths greater then 260 characters.