示例#1
0
"""

"""
import re
from fileasobj import FileAsObj

try:
    impossible = FileAsObj('File does not exist')
except FileNotFoundError as msg:
    print(msg)

test_file = FileAsObj('Test.txt', verbose=True)

#
# test iterable
for this in test_file:
    print(this)

print(test_file.birthday)

# test_file = FileAsObj('Test.txt')
# print(test_file)
# print(test_file.trace)

if 'Checking for __contains__ functionality 123' in test_file:
    print('__contains__ test string present')

if 'a7s6d9f7a6sd9f76asf9a8d7s89d6f967adfsadf' not in test_file:
    print('bogus string not in test file, good!')

test_file + 'using __add__ three times, force unique'
示例#2
0
"""
fileasobj/examples.py

"""
import sys
import os

from fileasobj import FileAsObj

# Reading a file
my_file = FileAsObj()
try:
    my_file.read('./input.txt')
except Exception as msg:
    print('File was NOT loaded, here are the errors')
    print(msg)

# Reading a file during instantiation and catching errors.
try:
    my_file = FileAsObj(os.path.join(os.sep, 'home', 'bob', 'clients.txt'),
                        verbose=True)
except Exception as msg:
    print(msg)
    sys.exit(10)

#
# If your file does not yet exist...
new_file = FileAsObj()
new_file.filename = './new_file.txt'
new_file.add('new data')
new_file.save()  # This will create the file on disk and put your data into it.