kely789456123
kely789456123

Reputation: 595

How to retrieve the full name of a file in a full path?

I have a path like this one :

path = "./corpus_test/corpus_ix_test_FMC.xlsx"

I want to retrieve the name of the file without ".xlsx" and the other parts of the file.

I know I should use index like this but there are some cases the file is different ans the path is not the same , for example :

path2 = "./corpus_ix/corpus_polarity_test_FMC.xlsx"

I am looking for a regular expression or a method which with retrieve only the name in both cases. for example, if I read a full repertory, there with lot of files and using index won't help me. Is there a way to do it in python and telling it it should start slicing at the last "/" ? so that I only retrieve the index of "/" and add "1" to start from.

what I try but still thinking

path ="./corpus_test/corpus_ix_test_FMC.xlsx"

list_of_index =[]
for f in path:
    if f == "/":
        ind = path.index(f)
        list_of_index.append(ind)

ind_to_start_count = max(list_of_index) + 1

print(" the index of the last "/" is" : 
name_of_file = path[ind_to_start_count:-5] # 

But the printing give me 1 for each / , is there a way to have the index of the letters part of the string ?

But the index of / in both case is 1 for each r ?

wanted to split in caracter but get error with

path ="./corpus_test/corpus_ix_test_FMC.xlsx"

path_string = path.split("")
print(path_string)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-b8bdc29c19b1> in <module>
      1 path ="./corpus_test/corpus_ix_test_FMC.xlsx"
      2 
----> 3 path_string = path.split("")
      4 print(path_string)

ValueError: empty separator

Upvotes: 0

Views: 47

Answers (2)

csulli245
csulli245

Reputation: 140

This is what I've been using:

def get_suffix(word, delimeter):
    """ Returns the part of word after the last instance of 'delimeter' """
    while delimeter in word:
        word = word.partition(delimeter)[2]
    return word


def get_terminal_path(path):
    """
    Returns the last step of a path
    Edge cases:
    -Delimeters: / or \\ or mixed
    -Ends with delimeter or not
    """
    # Convert "\\" to "/"
    while "\\" in path:
        part = path.partition("\\")
        path = part[0] + "/" + part[2]
    # Check if ends with delimeter
    if path[-1] == "/":
        path = path[0:-1]
    # Get terminal path
    out = get_suffix(path, "/")
    return out.partition(".")[0]

Upvotes: 0

SEKTION
SEKTION

Reputation: 87

import os

fullpath = r"./corpus_test/corpus_ix_test_FMC.xlsx"
full_filename = os.path.basename(fullpath)
filename, ext = os.path.splitext(full_filename)

This would give you the base filename without the extension

Upvotes: 1

Related Questions