Binary or ascii file?

Grant Edwards ge at nowhere.none
Fri Sep 8 09:54:53 EDT 2000


In article <c56hrss186d15ek3bo87okp2pk5orrc74s at 4ax.com>, Anders M Eriksson wrote:

>>>Working on windows I need to know if the file I'm about to open is
>>>binary or ascii.
>>
>>It's whichever you open it as.  "Binary vs. ASCII" mode is
>>determined by the method the program uses to handle the data in
>>the file. A file itself isn't either one, it's just a stream of
>>bytes.
>
>OK, I probably didn't make any sence in my original question ;-)
>
>I'm creating an app that uses FTP to send files. If the
>ftp-server is on a unix based computer then it will not work to
>send Windows ASCII files as binary (.by not work I mean that
>it's looks funny reading the file on the unix system.)

Ah.  That's tough.  Basically, you have to open the file, read
it, and look to see if the contents are all ASCII printable
characters (allowing tab, cr/lf, and maybe form-feed).  Of
course, depending on the char-set, it may be a text file and
still have non-ASCII characters in it (e.g. øéçöß).

Some files (e.g. RTF and Postscript), may be all printable
ASCII characters and _should_ tolerate having CR/LF
translations done, but I wouldn't.

For some file types, there are more easily identifiable
features in the first few bytes that can be used to determine
the file type.  The "file" program under Unix uses a whole heap
of heuristic rules (typically stored in /usr/share/magic) to
try to figure out types for lots of obscure file types.

If you're on a Unix system, try doing "man file" or "man
magic".

-- 
Grant Edwards                   grante             Yow!  PARDON me, am I
                                  at               speaking ENGLISH?
                               visi.com            



More information about the Python-list mailing list