本文整理汇总了Python中fileinput.FileInput方法的典型用法代码示例。如果您正苦于以下问题:Python fileinput.FileInput方法的具体用法?Python fileinput.FileInput怎么用?Python fileinput.FileInput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fileinput
的用法示例。
在下文中一共展示了fileinput.FileInput方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: modify_configmap
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def modify_configmap(regexes: List[(str)], experiment_index: int, experiment_root_dir: str):
path = '../wca-scheduler/'
config_name = 'config.yaml'
# Replace text in config
for regex in regexes:
with fileinput.FileInput(path + config_name, inplace=True) as file:
for line in file:
# regexes[0] - regex_to_search, regexes[1] - replacement_text
print(re.sub(regex[0], regex[1], line), end='')
# Make copy config
copyfile(path + config_name,
experiment_root_dir + '/' + 'wca_scheduler_config_'
+ str(experiment_index) + '_' + config_name)
# Apply changes
command = "kubectl apply -k {path_to_kustomize_folder_wca_scheduler} " \
"&& sleep {sleep_time}".format(
path_to_kustomize_folder_wca_scheduler=path,
sleep_time='10s')
default_shell_run(command)
switch_extender(OnOffState.Off)
示例2: test_zero_byte_files
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_zero_byte_files(self):
try:
t1 = writeTmp(1, [""])
t2 = writeTmp(2, [""])
t3 = writeTmp(3, ["The only line there is.\n"])
t4 = writeTmp(4, [""])
fi = FileInput(files=(t1, t2, t3, t4))
line = fi.readline()
self.assertEqual(line, 'The only line there is.\n')
self.assertEqual(fi.lineno(), 1)
self.assertEqual(fi.filelineno(), 1)
self.assertEqual(fi.filename(), t3)
line = fi.readline()
self.assertFalse(line)
self.assertEqual(fi.lineno(), 1)
self.assertEqual(fi.filelineno(), 0)
self.assertEqual(fi.filename(), t4)
fi.close()
finally:
remove_tempfiles(t1, t2, t3, t4)
示例3: test_readline
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_readline(self):
with open(TESTFN, 'wb') as f:
f.write('A\nB\r\nC\r')
# Fill TextIOWrapper buffer.
f.write('123456789\n' * 1000)
# Issue #20501: readline() shouldn't read whole file.
f.write('\x80')
self.addCleanup(safe_unlink, TESTFN)
fi = FileInput(files=TESTFN, openhook=hook_encoded('ascii'))
# The most likely failure is a UnicodeDecodeError due to the entire
# file being read when it shouldn't have been.
self.assertEqual(fi.readline(), u'A\n')
self.assertEqual(fi.readline(), u'B\r\n')
self.assertEqual(fi.readline(), u'C\r')
with self.assertRaises(UnicodeDecodeError):
# Read to the end of file.
list(fi)
fi.close()
示例4: test_modes
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_modes(self):
with open(TESTFN, 'wb') as f:
# UTF-7 is a convenient, seldom used encoding
f.write('A\nB\r\nC\rD+IKw-')
self.addCleanup(safe_unlink, TESTFN)
def check(mode, expected_lines):
fi = FileInput(files=TESTFN, mode=mode,
openhook=hook_encoded('utf-7'))
lines = list(fi)
fi.close()
self.assertEqual(lines, expected_lines)
check('r', [u'A\n', u'B\r\n', u'C\r', u'D\u20ac'])
check('rU', [u'A\n', u'B\r\n', u'C\r', u'D\u20ac'])
check('U', [u'A\n', u'B\r\n', u'C\r', u'D\u20ac'])
check('rb', [u'A\n', u'B\r\n', u'C\r', u'D\u20ac'])
示例5: remove_empty_lines_from_file
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def remove_empty_lines_from_file(fn):
""" Remove empty lines from a text file
Args:
fn - filename [string]
Returns: nothing
Has side effect of removing empty lines from file specified by fn
"""
f = fileinput.FileInput(fn, inplace=True)
for line in f:
stripped_line = line.rstrip()
if stripped_line:
print(stripped_line)
f.close()
示例6: test_readline
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_readline(self):
with open(TESTFN, 'wb') as f:
f.write('A\nB\r\nC\r')
# Fill TextIOWrapper buffer.
f.write('123456789\n' * 1000)
# Issue #20501: readline() shouldn't read whole file.
f.write('\x80')
self.addCleanup(safe_unlink, TESTFN)
fi = FileInput(files=TESTFN, openhook=hook_encoded('ascii'), bufsize=8)
# The most likely failure is a UnicodeDecodeError due to the entire
# file being read when it shouldn't have been.
self.assertEqual(fi.readline(), u'A\n')
self.assertEqual(fi.readline(), u'B\r\n')
self.assertEqual(fi.readline(), u'C\r')
with self.assertRaises(UnicodeDecodeError):
# Read to the end of file.
list(fi)
fi.close()
示例7: download_with_info_file
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def download_with_info_file(self, info_filename):
with contextlib.closing(fileinput.FileInput(
[info_filename], mode='r',
openhook=fileinput.hook_encoded('utf-8'))) as f:
# FileInput doesn't have a read method, we can't call json.load
info = self.filter_requested_info(json.loads('\n'.join(f)))
try:
self.process_ie_result(info, download=True)
except DownloadError:
webpage_url = info.get('webpage_url')
if webpage_url is not None:
self.report_warning('The info failed to download, trying with "%s"' % webpage_url)
return self.download([webpage_url])
else:
raise
return self._download_retcode
示例8: update_service
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def update_service(openpyn_options: str, run=False) -> None:
logger.debug(openpyn_options)
os.chmod("/opt/etc/init.d/S23openpyn", 0o755)
for line in fileinput.FileInput("/opt/etc/init.d/S23openpyn", inplace=1):
sline = line.strip().split("=")
if sline[0].startswith("ARGS"):
sline[1] = "\"" + openpyn_options + "\""
line = '='.join(sline)
logger.debug(line)
logger.notice("The Following config has been saved in S23openpyn. \
You can Start it or/and Stop it with: '/opt/etc/init.d/S23openpyn start', \
'/opt/etc/init.d/S23openpyn stop' \n")
if run:
logger.notice("Started Openpyn by running '/opt/etc/init.d/S23openpyn start'\n\
To check VPN status, run '/opt/etc/init.d/S23openpyn check'")
subprocess.run(["/opt/etc/init.d/S23openpyn", "start"])
示例9: parse_config
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def parse_config():
#Parses the configuration file removes blank lines , converts to redis protocol format to be sent to client
l=[]
output=open("redispot/config/redis2.conf","w")
input=open('redispot/config/redis.conf','r')
data=input.readlines()
for i in data:
if i.startswith('#'):
pass
else:
output.write(i)
output.close()
input.close()
for line in fileinput.FileInput("redispot/config/redis2.conf",inplace=1):
if line.rstrip():
print line
input=open('redispot/config/redis2.conf','r')
data=input.readlines()
for i in data:
if len(i.strip().split()) > 1:
l.append(i.strip().split())
print len(l)
red_enc_data=rediscommands.encode(l)
return red_enc_data
示例10: test_zero_byte_files
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_zero_byte_files(self):
t1 = t2 = t3 = t4 = None
try:
t1 = writeTmp(1, [""])
t2 = writeTmp(2, [""])
t3 = writeTmp(3, ["The only line there is.\n"])
t4 = writeTmp(4, [""])
fi = FileInput(files=(t1, t2, t3, t4))
line = fi.readline()
self.assertEqual(line, 'The only line there is.\n')
self.assertEqual(fi.lineno(), 1)
self.assertEqual(fi.filelineno(), 1)
self.assertEqual(fi.filename(), t3)
line = fi.readline()
self.assertFalse(line)
self.assertEqual(fi.lineno(), 1)
self.assertEqual(fi.filelineno(), 0)
self.assertEqual(fi.filename(), t4)
fi.close()
finally:
remove_tempfiles(t1, t2, t3, t4)
示例11: test_opening_mode
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_opening_mode(self):
try:
# invalid mode, should raise ValueError
fi = FileInput(mode="w")
self.fail("FileInput should reject invalid mode argument")
except ValueError:
pass
t1 = None
try:
# try opening in universal newline mode
t1 = writeTmp(1, [b"A\nB\r\nC\rD"], mode="wb")
with check_warnings(('', DeprecationWarning)):
fi = FileInput(files=t1, mode="U")
with check_warnings(('', DeprecationWarning)):
lines = list(fi)
self.assertEqual(lines, ["A\n", "B\n", "C\n", "D"])
finally:
remove_tempfiles(t1)
示例12: test_readline
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_readline(self):
with open(TESTFN, 'wb') as f:
f.write(b'A\nB\r\nC\r')
# Fill TextIOWrapper buffer.
f.write(b'123456789\n' * 1000)
# Issue #20501: readline() shouldn't read whole file.
f.write(b'\x80')
self.addCleanup(safe_unlink, TESTFN)
with FileInput(files=TESTFN,
openhook=hook_encoded('ascii'), bufsize=8) as fi:
try:
self.assertEqual(fi.readline(), 'A\n')
self.assertEqual(fi.readline(), 'B\n')
self.assertEqual(fi.readline(), 'C\n')
except UnicodeDecodeError:
self.fail('Read to end of file')
with self.assertRaises(UnicodeDecodeError):
# Read to the end of file.
list(fi)
self.assertEqual(fi.readline(), '')
self.assertEqual(fi.readline(), '')
示例13: test_nextfile_oserror_deleting_backup
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_nextfile_oserror_deleting_backup(self):
"""Tests invoking FileInput.nextfile() when the attempt to delete
the backup file would raise OSError. This error is expected to be
silently ignored"""
os_unlink_orig = os.unlink
os_unlink_replacement = UnconditionallyRaise(OSError)
try:
t = writeTmp(1, ["\n"])
self.addCleanup(remove_tempfiles, t)
with FileInput(files=[t], inplace=True) as fi:
next(fi) # make sure the file is opened
os.unlink = os_unlink_replacement
fi.nextfile()
finally:
os.unlink = os_unlink_orig
# sanity check to make sure that our test scenario was actually hit
self.assertTrue(os_unlink_replacement.invoked,
"os.unlink() was not invoked")
示例14: test_readline_os_fstat_raises_OSError
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_readline_os_fstat_raises_OSError(self):
"""Tests invoking FileInput.readline() when os.fstat() raises OSError.
This exception should be silently discarded."""
os_fstat_orig = os.fstat
os_fstat_replacement = UnconditionallyRaise(OSError)
try:
t = writeTmp(1, ["\n"])
self.addCleanup(remove_tempfiles, t)
with FileInput(files=[t], inplace=True) as fi:
os.fstat = os_fstat_replacement
fi.readline()
finally:
os.fstat = os_fstat_orig
# sanity check to make sure that our test scenario was actually hit
self.assertTrue(os_fstat_replacement.invoked,
"os.fstat() was not invoked")
示例15: test_readline_os_chmod_raises_OSError
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import FileInput [as 别名]
def test_readline_os_chmod_raises_OSError(self):
"""Tests invoking FileInput.readline() when os.chmod() raises OSError.
This exception should be silently discarded."""
os_chmod_orig = os.chmod
os_chmod_replacement = UnconditionallyRaise(OSError)
try:
t = writeTmp(1, ["\n"])
self.addCleanup(remove_tempfiles, t)
with FileInput(files=[t], inplace=True) as fi:
os.chmod = os_chmod_replacement
fi.readline()
finally:
os.chmod = os_chmod_orig
# sanity check to make sure that our test scenario was actually hit
self.assertTrue(os_chmod_replacement.invoked,
"os.fstat() was not invoked")