Search code examples
pythonfilepathlistdir

Trying to fetch all the files (including read only) from a directory using Python


I am new to Python - please excuse me if my question looks dumb.

I am trying to fetch all the files present in a given folder and create an Excel which shows file name and path.

It works fine on some folders, but not all. The reason which I think is maybe it's unable to fetch the data which is in read only mode (I am not quite sure of this).

Here is the error which I get:

F:/
Traceback (most recent call last):
File "c:/Vamsi_Folder/4.Tutorials/Python/OS/01-WIP/Get Files From Directory/Get_Files_From_Directory.py", line 36, in
List = getListOfFiles(filename)

File "c:/Vamsi_Folder/4.Tutorials/Python/OS/01-WIP/Get Files From Directory/Get_Files_From_Directory.py", line 30, in getListOfFiles
allFiles = allFiles + getListOfFiles(fullPath)

File "c:/Vamsi_Folder/4.Tutorials/Python/OS/01-WIP/Get Files From Directory/Get_Files_From_Directory.py", line 22, in getListOfFiles
listOfFile = os.listdir(dirName)

PermissionError: [WinError 5] Access is denied: 'F:/System Volume Information'

Here is the code which I am using:

#program to write files from directory in Excel


import os
from openpyxl import Workbook
from openpyxl.worksheet.table import Table, TableStyleInfo
import numpy as np
from tkinter import Tk
from tkinter.filedialog import askdirectory
from tkinter.filedialog import asksaveasfilename
import ntpath


Tk().withdraw()
filename = askdirectory()
print(filename)


def getListOfFiles(dirName):
    # create a list of file and sub directories 
    # names in the given directory 
    listOfFile = os.listdir(dirName)
    allFiles = list()
    # Iterate over all the entries
    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)
        # If entry is a directory then get the list of files in this directory 
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)

    return allFiles

List = getListOfFiles(filename)
Filename = []
for i in List:
    Filename.append(ntpath.basename(i))


workbook = Workbook()
worksheet = workbook.active
worksheet.title = "Directory"

outlist = [Filename,List]

transpose = np.array(outlist).T
transpose_outlist = transpose.tolist()

worksheet.append(["Filename","Path"])

for row in transpose_outlist:
    worksheet.append(row)

count = "A1:""B"+str(len(List)+1)

tab = Table(displayName="Directory", ref=count)
style = TableStyleInfo(name="TableStyleMedium9", showFirstColumn=False,
                       showLastColumn=False, showRowStripes=True, showColumnStripes=True)
tab.tableStyleInfo = style
worksheet.add_table(tab)

savename =asksaveasfilename(initialdir = "/",defaultextension="*.*",filetypes=\
                                     (("xml files","*.xml"),\
                                      ('xlsx files','.xlsx'),\
                                      ("all files","*.*")))

workbook.save(savename)

Solution

  • You don't have access to 'System Volume Information' of a hard disk partition. How to gain access to the System Volume Information folder

    Secondly, here is a better (pythonic) code, in my opinion, to get a list of all files in a directory and its subdirectories.

    import os
    import csv
    
    directory = r'F:/'
    
    files_list = [d + '\\' + f for d, dirs, files in os.walk(directory) for f in files]
    
    with open('dicrectory_files.csv', 'w', newline='') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['basename', 'filepath'])
        for file in files_list:
            csv_writer.writerow([os.path.basename(file), file])
    

    Then you can directly open this CSV in Excel.

    If you don't want to enter the directory path directly in your code then you can change the directory path assignment statement with the following.

    directory = input('Enter directory path: ')
    if directory == '':
        directory = os.getcwd()
    

    Then when you run the code file (.py), you just need to copy and paste the path of the directory you want to walk on in the prompt window. The CSV file will be created in the directory of your code file.