如何检查文件是否是文本文件?

问题描述 投票:22回答:3

Perl6是否有类似Perl5 -T file test的东西来判断文件是否是文本文件?

perl6 file-type
3个回答
20
投票

没有任何内置,但有一个模块Data::TextOrBinary这样做。

use Data::TextOrBinary;
say is-text('/bin/bash'.IO);                            # False
say is-text('/usr/share/dict/words'.IO);                # True

10
投票

那是has not been translated to Perl 6的启发式。你可以简单地用UTF8(或ASCII)读取它来做同样的事情:

given slurp("read-utf8.p6", enc => 'utf8') -> $f {
    say "UTF8";
}

(将read-utf8.p6替换为您要检查的文件的名称)


4
投票

我们可以使用以下代码来使用File :: Type。

use strict;
use warnings;

use File::Type;

my $file      = '/path/to/file.ext';
my $ft        = File::Type->new();
my $file_type = $ft->mime_type($file);

if ( $file_type eq 'application/octet-stream' ) {
    # possibly a text file
}
elsif ( $file_type eq 'application/zip' ) {
    # file is a zip archive
}

资料来源:https://metacpan.org/pod/File::Type

© www.soinside.com 2019 - 2024. All rights reserved.