Binary or ascii file?

Steve Holden sholden at holdenweb.com
Fri Sep 8 11:47:00 EDT 2000


Anders M Eriksson wrote:
> 
> On Thu, 07 Sep 2000 17:50:46 GMT, ge at nowhere.none (Grant Edwards)
> wrote:
> 
> >In article <imefrs4r6svcpk7rsa617ps45u3eepi2hb 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.)
> 
> I would like to be able to automaticle select binary or ASCII mode.
> Which then means that I need to find out if the file is an ASCII file
> or not.
> 
> // Anders

Anders:

There's no guaranteed algorithm.  Assuming you aren't able to decide
simply on the basis of filename extensions, probably the best way is
to scan the file you are planning to send for the DOS line ending
(CR/LF, or in Python "\015\012").  If this string occurs periodically,
with no two occurrences further apart than some maximum line length,
then you have a file to be transferred in ASCII.

regards
 Steve
-- 
Helping people meet their information needs with training and technology.
703 967 0887      sholden at bellatlantic.net      http://www.holdenweb.com/



More information about the Python-list mailing list