Need to know if a file as only ASCII charaters

Dave Angel davea at ieee.org
Tue Jun 16 09:17:42 EDT 2009


Jorge wrote:
> Hi there,
> I'm making  a application that reads 3 party generated ASCII files, but some
> times
> the files are corrupted totally or partiality and I need to know if it's a
> ASCII file with *nix line terminators.
> In linux I can run the file command but the applications should run in
> windows.
>
> Any help will be great.
>
> Thank you in advance.
>
>   
So, which is the assignment:
   1) determine if a file has non-ASCII characters
   2) determine whether the line-endings are crlf or just lf

In the former case, look at translating the file contents to Unicode, 
specifying ASCII as source.  If it fails, you have non-ASCII
In the latter case, investigate the 'u' attribute of the mode parameter 
in the open() function.

You also need to ask yourself whether you're doing a validation of the 
file, or doing a "best guess" like the file command.





More information about the Python-list mailing list