本文整理汇总了Python中fileasobj.FileAsObj.read方法的典型用法代码示例。如果您正苦于以下问题:Python FileAsObj.read方法的具体用法?Python FileAsObj.read怎么用?Python FileAsObj.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fileasobj.FileAsObj
的用法示例。
在下文中一共展示了FileAsObj.read方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: example_read_catch_errors
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import read [as 别名]
def example_read_catch_errors():
""" Reading a file and catch errors. """
try:
my_file = FileAsObj()
my_file.read('/tmp/example_file.txt')
except Exception as msg:
print(msg)
示例2: example_sort_during_read
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import read [as 别名]
def example_sort_during_read():
"""
To sort contents during read().
The .sorted attribute is checked every time contents are modified.
Whenever a change occurs if sorted is True the contents are sorted with self.sort().
"""
my_file = FileAsObj()
my_file.sorted = True
my_file.read('/tmp/example_file.txt')
示例3: test_unique_failure_during_read
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import read [as 别名]
def test_unique_failure_during_read(self):
""" Test wrong attribute type of self.unique during read() """
test_file = FileAsObj()
test_file.filename = TESTFILE
test_file.contents = BLANKFILE.split('\n')
self.assertTrue(test_file.save())
test_file = FileAsObj()
test_file.unique = 'this is invalid attr type'
with self.assertRaises(AttributeError):
test_file.read(TESTFILE)
示例4: test_blank_file_without_unique
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import read [as 别名]
def test_blank_file_without_unique(self):
""" Testing not-unique on empty lines during .read() """
test_file = FileAsObj()
test_file.filename = TESTFILE
test_file.contents = BLANKFILE.split('\n')
self.assertTrue(test_file.save())
test_file = FileAsObj()
test_file.unique = False
test_file.read(TESTFILE)
self.assertTrue(test_file.contents == ['', '', '', ''])
示例5: test_param_failure
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import read [as 别名]
def test_param_failure(self):
""" Passing anything other than String as filename should raise TypeError. """
with self.assertRaises(TypeError):
FileAsObj(1)
with self.assertRaises(TypeError):
FileAsObj(True)
with self.assertRaises(TypeError):
FileAsObj([])
with self.assertRaises(TypeError):
FileAsObj({})
with self.assertRaises(TypeError):
test_file = FileAsObj()
test_file.read(False)
示例6: FileAsObj
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import read [as 别名]
"""
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')