且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

确定文件类型(二进制文本或文本)

更新时间:2023-02-08 18:48:17

>如何检查文件是二进制还是文本?
> How can I check if a file is binary or text?
import os
f = os。 popen(''file -bi test.py'',''r'')
f.read()。startswith(''text'')
import os
f = os.popen(''file -bi test.py'', ''r'')
f.read().startswith(''text'')

b $ b 1


(btw,f.read()返回''text / x-java; charset = us-ascii \ n'')


-

bromden [at] gazeta.pl


1

(btw, f.read() returns ''text/x-java; charset=us-ascii\n'')

--
bromden[at]gazeta.pl


> >>> f = os.popen(''file -bi test.py'',''r'')
> >>> f = os.popen(''file -bi test.py'', ''r'')
>> ;> f.read()。startswith(''text'')


抱歉,这不是一般的,因为file -i返回

" application / x-shellscript"对于shell脚本,

它***这样:import os
f = os.popen(''file test.py'',''r'')
f.read()。find(''text'')!= -1
>>> f.read().startswith(''text'')
sorry, it''s not general, since "file -i" returns
"application/x-shellscript" for shell scripts,
it''s better to go like that: import os
f = os.popen(''file test.py'', ''r'')
f.read().find(''text'') != -1




-

bromden [at] gazeta.pl



--
bromden[at]gazeta.pl


在Unix中运行良好,但我正在创建一个适用于两者的脚本>
Unix和Windows。


Win没有''file -bi''命令。


" ; bromden" &LT峰; br ***** @ gazeta.pl.no.spam>在消息中写道

news:bh ********** @ absinth.dialog.net.pl ...
Works well in Unix but I''m making a script that works on both
Unix and Windows.

Win doesn''t have that ''file -bi'' command.

"bromden" <br*****@gazeta.pl.no.spam> wrote in message
news:bh**********@absinth.dialog.net.pl...
如何检查文件是二进制还是文本?
How can I check if a file is binary or text?


>>> import os
>>> f = os.popen(''file -bi test.py'',''r'')
>>> f.read()。startswith(''text'')
>>> import os
>>> f = os.popen(''file -bi test.py'', ''r'')
>>> f.read().startswith(''text'')


1

(btw,f.read()返回''text / x- java; charset = us-ascii \ n'')

-
bromden [at] gazeta.pl


1

(btw, f.read() returns ''text/x-java; charset=us-ascii\n'')

--
bromden[at]gazeta.pl