如何从目录名+文件中获取目录名

时间:2019-02-24 11:54:31

标签: python-3.x string

在应用程序中,我可以以字符串的形式获取位于目录中的文件的路径:

"/path/to/the/file.txt"

为了将另一个文件写入同一目录,我想更改字符串"/path/to/the/file.txt"并删除部分"file.txt"以最终只得到

"/path/to/the/"

作为字符串

我可以使用

string  = "/path/to/the/file.txt"
string.split('/')

,然后将所有术语(最后一个除外)与一个循环粘在一起

有简单的方法吗?

2 个答案:

答案 0 :(得分:1)

您可以使用random获取路径的最后一部分,并使用os.path.basename删除它。

replace

输出:

import os
path = "/path/to/the/file.txt"

delete = os.path.basename(os.path.normpath(path))
print(delete) # will return file.txt
#Remove file.txt in path
path = path.replace(delete,'')
print(path)

假设您有一个包含txt文件的数组。您可以得到所有类似的路径

file.txt
/path/to/the/ 

输出:

new_path = ['file2.txt','file3.txt','file4.txt']
for get_new_path in new_path:
    print(path + get_new_path)

答案 1 :(得分:0)

这是我最终使用的

function [resVectorDirection, magnitudeNormal, actualNormal] = estSurfNorm(newImg)
% Define empty array
resVectorDirection = [];
magnitudeNormal = [];
actualNormal = [];
for i = 2:size(newImg, 1) - 1
    for j = 2:size(newImg, 2) - 1

        % Obtain value from 8-neighborhood matrix
        imgNeighborhood(1) = newImg(i-1,j-1);
        imgNeighborhood(2) = newImg(i-1,j);
        imgNeighborhood(3) = newImg(i-1,j+1);
        imgNeighborhood(4) = newImg(i,j-1);
        imgNeighborhood(5) = newImg(i,j+1);
        imgNeighborhood(6) = newImg(i+1,j-1);
        imgNeighborhood(7) = newImg(i+1,j);
        imgNeighborhood(8) = newImg(i+1,j+1);

        % Perform sort operation for array
        [value, index] = sort(imgNeighborhood, 'descend');
        % Obtain total largest value in array
        totalLargestValue = sum(ismember(imgNeighborhood, value(1)));
        % Obtain all largest value in array
        largestValue = value(1:totalLargestValue);
        % Obtain all index largest value in array
        indexLargestValue = index(1:totalLargestValue);

        % Check if there are multiple value from 8-neighborhood matrix
        if (totalLargestValue > 1)
            for k = 1:totalLargestValue
                if (indexLargestValue(k) == 1)
                    multiVectorDirection(k, 1) = (i-1) - i;
                    multiVectorDirection(k, 2) = (j-1) - j;
                elseif (indexLargestValue(k) == 2)
                    multiVectorDirection(k, 1) = (i-1) - i;
                    multiVectorDirection(k, 2) = j - j;
                elseif (indexLargestValue(k) == 3)
                    multiVectorDirection(k, 1) = (i-1) - i;
                    multiVectorDirection(k, 2) = (j+1) - j;
                elseif (indexLargestValue(k) == 4)
                    multiVectorDirection(k, 1) = i - i;
                    multiVectorDirection(k, 2) = (j-1) - j;
                elseif (indexLargestValue(k) == 5)
                    multiVectorDirection(k, 1) = i - i;
                    multiVectorDirection(k, 2) = (j+1) - j;
                elseif (indexLargestValue(k) == 6)
                    multiVectorDirection(k, 1) = (i+1) - i;
                    multiVectorDirection(k, 2) = (j-1) - j;
                elseif (indexLargestValue(k) == 7)
                    multiVectorDirection(k, 1) = (i+1) - i;
                    multiVectorDirection(k, 2) = j - j;
                elseif (indexLargestValue(k) == 8)
                    multiVectorDirection(k, 1) = (i+1) - i;
                    multiVectorDirection(k, 2) = (j+1) - j;
                end
            end
            % Calculate suface normal direction and magnitude for multiple
            % direction
            tempVectorDirection = sum(multiVectorDirection, 1);
            resVectorDirection = [resVectorDirection; tempVectorDirection];
            actualNormal = [actualNormal; i j tempVectorDirection(1)+i tempVectorDirection(2)+j];
            magnitudeNormal = [magnitudeNormal; largestValue(1)];
        elseif (totalLargestValue == 1)
            if (indexLargestValue == 1)
                singleVectorDirection(1) = (i-1) - i;
                singleVectorDirection(2) = (j-1) - j;
            elseif (indexLargestValue == 2)
                singleVectorDirection(1) = (i-1) - i;
                singleVectorDirection(2) = j - j;
            elseif (indexLargestValue == 3)
                singleVectorDirection(1) = (i-1) - i;
                singleVectorDirection(2) = (j+1) - j;
            elseif (indexLargestValue == 4)
                singleVectorDirection(1) = i - i;
                singleVectorDirection(2) = (j-1) - j;
            elseif (indexLargestValue == 5)
                singleVectorDirection(1) = i - i;
                singleVectorDirection(2) = (j+1) - j;
            elseif (indexLargestValue == 6)
                singleVectorDirection(1) = (i+1) - i;
                singleVectorDirection(2) = (j-1) - j;
            elseif (indexLargestValue == 7)
                singleVectorDirection(1) = (i+1) - i;
                singleVectorDirection(2) = j - j;
            elseif (indexLargestValue == 8)
                singleVectorDirection(1) = (i+1) - i;
                singleVectorDirection(2) = (j+1) - j;
            end
            % If surface direction is only one direction, then assign it to
            % actual surface normal and magnitude
            resVectorDirection = [resVectorDirection; singleVectorDirection];
            actualNormal = [actualNormal; i j singleVectorDirection(1)+i singleVectorDirection(2)+j];
            magnitudeNormal = [magnitudeNormal; largestValue];
        end
    end
end

end