Python fileinput.FileInput() Examples
The following are 30
code examples of fileinput.FileInput().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
fileinput
, or try the search function
.
Example #1
Source File: test_fileinput.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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)
Example #2
Source File: test_fileinput.py From ironpython2 with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: test_fileinput.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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")
Example #4
Source File: test_fileinput.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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")
Example #5
Source File: test_fileinput.py From BinderFilter with MIT License | 6 votes |
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)
Example #6
Source File: test_fileinput.py From ironpython2 with Apache License 2.0 | 6 votes |
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'])
Example #7
Source File: test_fileinput.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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")
Example #8
Source File: test_fileinput.py From ironpython3 with Apache License 2.0 | 6 votes |
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)
Example #9
Source File: so_strip_cfg.py From TOOLS with GNU General Public License v3.0 | 6 votes |
def pP2(self): print('Reconfiguring pulledpork.conf.') with fileinput.FileInput(self.iS.pP, inplace=True) as file: for line in file: print(line.replace('#' + self.COMR, self.COMR), end='') for entry in self.liSt: with fileinput.FileInput(self.iS.pP, inplace=True) as file: for line in file: print(line.replace(entry, '#' + entry), end='') with fileinput.FileInput(self.iS.pP, inplace=True) as file: for line in file: print(line.replace(self.ipbOLD, self.ipbNEW), end='') with fileinput.FileInput(self.iS.pP, inplace=True) as file: for line in file: print(line.replace(self.iprOLD, self.iprNEW), end='') with fileinput.FileInput(self.iS.pP, inplace=True) as file: for line in file: print(line.replace(self.ettOLD, self.ettNEW), end='') open('/etc/nsm/rules/iplistsIPRVersion.dat', 'a+').close()
Example #10
Source File: test_fileinput.py From ironpython2 with Apache License 2.0 | 6 votes |
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()
Example #11
Source File: so_strip_cfg.py From TOOLS with GNU General Public License v3.0 | 6 votes |
def pubInput(self): homeNet = input('What is your public IP address subnet? : ') answeR = input(homeNet + ' selected. Confirm? [Y/n]: ') if (answeR == 'y' or answeR == ''): pass elif (answeR == 'n'): self.pubInput() else: print('Invalid entry. Try again') self.pubInput() with fileinput.FileInput(self.iS.snO, inplace=True) as file: for line in file: print(line.replace('ipvar HOME_NET [192.168.0.0/16,10.0.0.0/8,172.16.0.0/12]', 'ipvar HOME_NET [192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,' + homeNet + ']'), end='') return() ## -- SCRIPT PREP, STOP SERVICES AND DISABLE ALL BUT NSM MODS -- ##
Example #12
Source File: test_fileinput.py From ironpython3 with Apache License 2.0 | 6 votes |
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)
Example #13
Source File: helpers.py From opensauce-python with Apache License 2.0 | 6 votes |
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()
Example #14
Source File: runner.py From workload-collocation-agent with Apache License 2.0 | 6 votes |
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)
Example #15
Source File: test_fileinput.py From ironpython3 with Apache License 2.0 | 6 votes |
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(), '')
Example #16
Source File: test_fileinput.py From ironpython3 with Apache License 2.0 | 6 votes |
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")
Example #17
Source File: test_fileinput.py From oss-ftp with MIT License | 6 votes |
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()
Example #18
Source File: test_fileinput.py From oss-ftp with MIT License | 6 votes |
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'])
Example #19
Source File: test_fileinput.py From ironpython3 with Apache License 2.0 | 6 votes |
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")
Example #20
Source File: test_fileinput.py From ironpython3 with Apache License 2.0 | 6 votes |
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")
Example #21
Source File: YoutubeDL.py From tvalacarta with GNU General Public License v3.0 | 6 votes |
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
Example #22
Source File: initd.py From openpyn-nordvpn with GNU General Public License v3.0 | 6 votes |
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"])
Example #23
Source File: YoutubeDL.py From youtube-dl-GUI with MIT License | 6 votes |
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
Example #24
Source File: redisconfig.py From nosqlpot with GNU General Public License v2.0 | 6 votes |
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
Example #25
Source File: test_fileinput.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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)
Example #26
Source File: test_fileinput.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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(), '')
Example #27
Source File: test_fileinput.py From oss-ftp with MIT License | 6 votes |
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)
Example #28
Source File: test_fileinput.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_close_on_exception(self): try: t1 = writeTmp(1, [""]) with FileInput(files=t1) as fi: raise OSError except OSError: self.assertEqual(fi._files, ()) finally: remove_tempfiles(t1)
Example #29
Source File: test_fileinput.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_empty_files_list_specified_to_constructor(self): with FileInput(files=[]) as fi: self.assertEqual(fi._files, ('-',))
Example #30
Source File: test_fileinput.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_fileno(self): t1 = t2 = None try: t1 = writeTmp(1, ["A\nB"]) t2 = writeTmp(2, ["C\nD"]) fi = FileInput(files=(t1, t2)) self.assertEqual(fi.fileno(), -1) line =next( fi) self.assertNotEqual(fi.fileno(), -1) fi.nextfile() self.assertEqual(fi.fileno(), -1) line = list(fi) self.assertEqual(fi.fileno(), -1) finally: remove_tempfiles(t1, t2)