如何在Windows中使用Perl获取没有文件名的路径?

mit*_*050 4 windows perl filenames

我希望在Windows中通过perl获取没有文件名的路径

我有这样的完整路径:

c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do

我只需要路径,并希望剥离文件名,如:

c:\My_Designs\ipcore_script\test\src\IP_CORE\mux

TLP*_*TLP 7

使用File :: Basename

> perl -MFile::Basename -wE "say dirname(qq(c:/perl/bin/perl.exe));"
c:/perl/bin
Run Code Online (Sandbox Code Playgroud)

  • File :: Spec是另一种选择.(几年前我从File :: Basename切换到File :: Spec;如果我记得为什么会这样做,我会跟进.) (2认同)

Sin*_*nür 5

您还可以使用Path :: Class:

#!/usr/bin/env perl

use strict;
use warnings;

use Path::Class;

my $file = file('c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do');

print $file->parent, "\n";
Run Code Online (Sandbox Code Playgroud)

输出:

C:\My_Designs\ipcore_script\test\src\IP_CORE\mux