Home > Software design >  Files Directory with Folder name and link to file creating a list
Files Directory with Folder name and link to file creating a list

Time:10-19

import os

for folderName, subFolders, fileNames in os.walk('C:\Home\Homework\Folders'):
    for file in fileNames:
        print(folderName, os.path.join(folderName, file))

Let's say that there are 3 folders Named One, Two, and Three that live in C:\Home\Homework\Folders.

I want a script that will create a list or table such as:

Folder File Link
One C:\Home\Homework\Folders\One\sample.pdf.
One C:\Home\Homework\Folders\One\sample.txt
Two C:\Home\Homework\Folders\Two\test1.pdf
Two C:\Home\Homework\Folders\Two\test.csv
Three C:\Home\Homework\Folders\Three\excel.xlsx

My end goal is to export a list to a CSV file.

CodePudding user response:

As I mentioned in the comments, it's not clear to me whether you intend to walk subdirectories too. If so, you'll need a different solution.

I believe the following would achieve your task, however I did not test this code.

import os
import csv


# Open your output file in a context manager
with open("my_output.csv", "w", newline="") as output_file:
    # Create a DictWriter instance using your output file and your desired header
    writer = csv.DictWriter(output_file, fieldnames=("Folder", "File Link"))
    # Write the header to the file
    writer.writeheader()
    
    # Walk the target directory
    for folder_name, _, file_names in os.walk('C:\Home\Homework\Folders'):
        for file_name in file_names:
            file_link = os.path.join(folder_name, file_name)
            # Construct a dict whose keys match the `fieldnames` from earlier
            writer.writerow({"Folder": folder_name, "File Link": file_link})

Note the _ in the top-level for loop. This is because, as I understand your question, you don't seem to want to walk the subdirectories. Using _ as a variable name in Python is a conventional way of signifying to others reading your code that you do not care about the value and are not planning on using it anywhere. In this case _ will be a list of directory names (or empty if there are no subdirectories in a given directory from C:\Home\Homework\Folders).

For example, suppose your file structure is

C/
  Home/
    Homework/
      Folders/
        One/
          sample.pdf
          sample.txt
          SomeSubDirectory/
            some_file.py

You wouldn't find some_file.py because we're not walking SomeSubDirectory.

  • Related