Read specific folder's name from folder's path Python

Question:

I’m trying to read the folder name at the specific place from the file path. My current code:

import os

# search for and input multiple files 
def get_files(source):
    matches = []
    for root, dirnames, filenames in os.walk(source):
        for filename in filenames:
            matches.append(os.path.join(root, filename))
    return matches

def parse(files):
    for file in files:
        xml_information = {}
        metadata = []
        # Get the file path
        filepath = os.path.dirname(file)
        xml_information['file_path'] = '%s' % filepath
        
        # Get customer name
        customer = filepath.split("\")[5]
        xml_information['customer_name'] = '%s' % customer
        metadata.append(xml_information)
        print(metadata)

path = 'C:\Users\quan.nguyen\SAGE\Lania Thompson - Searching Project Files'

parse(get_files(path))

My program searches through folders and find the files and report back their folder path. However, I would like to read the folder path as well as the folder name at the sixth place which is customer name. When I run customer = filepath.split("\")[5] it report an error:

Traceback (most recent call last):
  File "*hidden*", line 33, in <module>
    parse(get_files(path))
  File "*hidden*", line 26, in parse
    customer = filepath.split("\")[5]
               ~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

However, when I run with customer = filepath.split("\")[4], the program works and reads the last folder specified in path which is Lania Thompson – Searching Project Files. The result is as follows:

[{'file_path': 'C:\Users\quan.nguyen\SAGE\Lania Thompson - Searching Project Files\Hazor Ltd\PCS Mah\Machine', 'customer_name': 'Lania Thompson - Searching Project Files'}]

My expecting result is Hazor Ltd:

[{'file_path': 'C:\Users\quan.nguyen\SAGE\Lania Thompson - Searching Project Files\Hazor Ltd\PCS Mah\Machine', 'customer_name': 'Hazor Ltd'}]

The names are made up except from my name

Asked By: Quan Hoang Nguyen

||

Answers:

So, I have worked out the code. The code is:

import os

# search for and input multiple files 
def get_files(source):
    matches = []
    for root, dirnames, filenames in os.walk(source):
        for filename in filenames:
            matches.append(os.path.join(root, filename))
    return matches

def parse(files):
    for file in files:
        # Get the file path
        absolutefilepath = os.path.abspath(file)
        filepath = os.path.dirname(absolutefilepath)
        try:
            parts = filepath.split(os.sep)
            customer = parts[1]
            print(customer)
        except:
            continue

path = 'C:\Users\quan.nguyen\SAGE\Lania Thompson - Searching Project Files'

parse(get_files(path))

change the number [5] in xml_information['Customer'] = f.parts[5] for the place you want to get the folder’s name from. Try and except is there so that if the folder at the specified location does not exist then continue the loop.

Answered By: Quan Hoang Nguyen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.