Given the full path to a file, how do I get just the path without the filename?

后端 未结 2 543
小蘑菇
小蘑菇 2021-01-22 01:37

Suppose I have a path in a string called \'/home/user/directory/HelloWorld.txt\'. I would like to remove the HelloWorld.txt, and end up with \'/home/user/directory\'. What rege

2条回答
  •  没有蜡笔的小新
    2021-01-22 02:02

    split on "/", remove last element and join them back.

    $path='/home/user/directory/HelloWorld.txt';
    @s = split /\// ,$path;
    pop(@s);
    print join("/",@s);
    

提交回复
热议问题