mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
- Added a file dialog example
- Added pointers to library documentation
This commit is contained in:
parent
a547dcaff0
commit
024a387f89
4 changed files with 132 additions and 2 deletions
39
Mac/Demo/example0/checktext.py
Normal file
39
Mac/Demo/example0/checktext.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
"""checktext - Check that a text file has macintosh-style newlines"""
|
||||
|
||||
import macfs
|
||||
import sys
|
||||
import EasyDialogs
|
||||
import string
|
||||
|
||||
def main():
|
||||
fsspec, ok = macfs.PromptGetFile('File to check end-of-lines in:', 'TEXT')
|
||||
if not ok:
|
||||
sys.exit(0)
|
||||
pathname = fsspec.as_pathname()
|
||||
fp = open(pathname, 'rb')
|
||||
try:
|
||||
data = fp.read()
|
||||
except MemoryError:
|
||||
EasyDialogs.Message('Sorry, file is too big.')
|
||||
sys.exit(0)
|
||||
if len(data) == 0:
|
||||
EasyDialogs.Message('File is empty.')
|
||||
sys.exit(0)
|
||||
number_cr = string.count(data, '\r')
|
||||
number_lf = string.count(data, '\n')
|
||||
if number_cr == number_lf == 0:
|
||||
EasyDialogs.Message('File contains no lines.')
|
||||
if number_cr == 0:
|
||||
EasyDialogs.Message('File has unix-style line endings')
|
||||
elif number_lf == 0:
|
||||
EasyDialogs.Message('File has mac-style line endings')
|
||||
elif number_cr == number_lf:
|
||||
EasyDialogs.Message('File probably has MSDOS-style line endings')
|
||||
else:
|
||||
EasyDialogs.Message('File has no recognizable line endings (binary file?)')
|
||||
sys.exit(0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue