- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 test1.txt 的输入文件,其中包含成百上千个文件名。
test word document.docx
...
...
amazing c. document.docx
1. 2. 3.45 document.docx
...
...
我想做的是从字符串中获取文件名和扩展名。对于大多数文件名,只有一个点,因此我可以使用点作为分隔符来获取文件名和分机号。但问题是某些文件名在文件名中有多个点。我不知道如何从中获取扩展名和文件名。
这是我的 perl 代码。
use strict;
use warnings;
print "Perl Starting ... \n\n";
open my $input_filehandle1, , '<', 'test1.txt' or die "No input Filename Found test1.txt ... \n";
while (defined(my $recordLine = <$input_filehandle1>))
{
chomp($recordLine);
my @fields = split(/\./, $recordLine);
my $arrayCount = @fields;
#if the array size is more than 2 then we encountered multiple dots
if ($arrayCount > 2)
{
print "I dont know how to get filename and ext ... $recordLine ... \n";
}
else
{
print "FileName: $fields[0] ... Ext: $fields[1] ... \n";
}
}#end while-loop
print "\nPerl End ... \n\n";
1;
这是输出:
Perl Starting ...
FileName: test word document ... Ext: docx ...
I dont know how to get filename and ext ... amazing c. document.docx ...
I dont know how to get filename and ext ... 1. 2. 3.45 document.docx ...
Perl End ...
我想得到什么
FileName: test word document ... Ext: docx ...
FileName: amazing c. document ... Ext: docx ...
FileName: 1. 2. 3.45 document ... Ext: docx ...
最佳答案
这就是File::Basename是为了。
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use File::Basename;
while (<DATA>) {
chomp;
my ($name, undef, $ext) = fileparse($_, '.docx');
say "Filename: $name ... Ext: $ext";
}
__DATA__
test word document.docx
amazing c. document.docx
1. 2. 3.45 document.docx
三件值得解释的事情。
DATA
文件句柄,因为这是一个演示,它比使用单独的输入文件更容易。fileparse()
返回目录路径作为第二个值。由于此数据不包含目录路径,因此我忽略了该值(通过将其分配给 undef
)。fileparse()
的第二个(及后续)参数是要分离的扩展列表。您只在示例数据中使用一个扩展名。如果您有更多扩展名,您可以将它们添加到“.docx”之后。关于perl如何获取文件名和扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55905684/
我有一个 .crt 文件。打开那个文件,我看到它以开头 -----BEGIN CERTIFICATE----- 如何从这个文件生成这 2 个文件?: 以-----BEGIN CERTIFICATE R
我有一个文件被加载到 fd.ChosenName 并想知道是否有一个快速的解决方案,例如与 stringbuilder 相反的截断文件名(扩展名)以用于显示目的。因此,如果 fd.ChosenName
我的文件名如下:name1.csv,我想提取该字符串的两个子字符串。一种将 name1 存储在一个变量中,另一种将扩展名 csv 存储在另一个变量中,但不带点。 我一直在寻找是否有像 Java 的 i
我正在尝试找出一种方法来确定我在 struts2 应用程序中上传的文件的文件类型。我的应用程序可以读取 CSV 和 XML 文件并根据上传的文件类型执行操作。我将该文件作为“File”类的实例。该文件
如何使用 NLP/信息提取 (IE) 方法检测/找出首字母缩略词的含义(扩展名)? 我们希望检测自由文本中是否使用了单词或其首字母缩略词,并将其映射到相同的实体/标记。 大多数在线论文都是关于医学缩略
我正在编写一个 shell 脚本,该脚本需要改变其行为,并根据特定 X11 扩展的存在或不存在为被调用程序提供不同的选项。我有一个可行的解决方案,但我希望有一个更干净的解决方案。我愿意考虑一个简单的
我要查找文件名的扩展名,扩展名只能是3个字符。 有点像, filename = str(input("Please enter filename: ")) 然后我想执行一个任务来查找扩展名,如果扩展名
我正在开发一种文档管理系统。最终用户是企业级用户。 我目前仅检查并允许上传符合以下条件之一的文件: "png|jpe?g|gif|xls|doc|docx|csv|ppt|txt|pdf|rtf" 我
尝试将文件列表添加到 Jlist,然后过滤 JList 中的文件以仅返回 .txt 文件和固定字符长度。还尝试删除返回的文件路径,并仅在文件 JList 中显示文件名+扩展名。 到目前为止,除了删除文
File.py file = fields.Binary('File') @api.multi def get_file(self): if self.file: xsl_fi
我尝试使用导航 Controller 使我的应用程序处于纵向模式我收到此错误 iam usring Xcode7 and Swift 2 And Target System IOS 9.3 声明仅在文
这个问题在这里已经有了答案: Test whether a glob has any matches in Bash (22 个回答) 关闭去年。 您将如何使用 bash 判断目录中是否存在特定扩展名
我正在开发转换器服务,我使用 FFMPEG 转换视频。 我想检查输入文件,哪种真的是视频。 我使用以下命令并获取文件类型。但是如果文件已经被操作过就无法理解了: curl -s -o /dev/nul
我需要一个规则来添加 .html 扩展名,只要有“不”结尾的斜杠。 一个新客户最近更改了电子商务脚本,新版本以不同的方式处理 SEO,并更改了他们所有 16,000 多个产品链接。这在站点被重新索引之
我正在尝试使用 Kickstarter 进行扩展以覆盖页面的正常呈现,并呈现 PDF 文件。为此我使用 FPDF。但我不确定该怎么做。我尝试这样做,但没有成功: AddPage();
在寻找巴恩斯利蕨分形的实现时,我遇到了一个具有 .pde 扩展名的实现。哪种编程语言使用此扩展? Implementation Page 最佳答案 此代码来自Processing.org,一个基于Ja
Visual Studio Code (1.22.2) 提供了一个名为 .dockerfile 的文件扩展名。在保存对话框中。带有此扩展名的文件是什么? Dockerfile 包含在所有文档和示例中,
我正在为32位计算机编译一个应用程序,因为该应用程序的创建者将其设置为64位。我确保所有库和DLL都是32位的。但是,我在编译时遇到问题。当我使用sh make.sh进行编译时,当它进入GCC构建部分
当我将文件保存在 img/upload 文件夹中时,文件会以正确的文件扩展名保存。 但是,当我尝试下载该文件时,会附加一个 .htm 文件扩展名。 我怎样才能避免这种情况?我在下面添加了我的代码; V
GHC 有几种有用的语言 extensions用于机械派生各种常见的 Haskell 类型类(-XDeriveFunctor、-XDeriveFoldable、-XDeriveTraversable)
我是一名优秀的程序员,十分优秀!