确定文件类型(二进制文本或文本) [英] Determine file type (binary or text)

查看:90
本文介绍了确定文件类型(二进制文本或文本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我如何检查文件是二进制还是文本?


有一些简单的方法,但我忘记了..

感谢adv。

Hello,

How can I check if a file is binary or text?

There was some easy way but I forgot it..
Thanks in adv.

推荐答案

>如何检查文件是二进制还是文本?
> 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'')

< br $> 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" <峰; 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



这篇关于确定文件类型(二进制文本或文本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆