Asked  7 Months ago    Answers:  5   Viewed   42 times

Is it possible to parse a file line by line, and edit a line in-place while going through the lines?



Is it possible to parse a file line by line, and edit a line in-place while going through the lines?

It can be simulated using a backup file as stdlib's fileinput module does.

Here's an example script that removes lines that do not satisfy some_condition from files given on the command line or stdin:

#!/usr/bin/env python
import fileinput

for line in fileinput.input(inplace=True, backup='.bak'):
    if some_condition(line):
        print line, # this goes to the current file


$ python first_file.txt second_file.txt

On completion first_file.txt and second_file.txt files will contain only lines that satisfy some_condition() predicate.

Tuesday, June 1, 2021
answered 7 Months ago
  • In Windows PowerShell as of v5.1, there is NO built-in way to avoid creating a BOM with UTF-8 encoding (short of calling the .NET framework directly, as you demonstrate).

    • In v5.1+ you can change the default encoding for > / >> as follows, but if you choose utf8, you still get a BOM:

      • $PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
    • Consider use of third-party function Out-FileUtf8NoBom from this answer of mine.

    • Unfortunately, it is unlikely that Windows PowerShell will ever support creation of BOM-less UTF-8 files[1] , but the hope is that PowerShell Core, which not only supports that but also defaults to interpreting BOM-less files as UTF-8 (see below), will eventually be a viable alternative on Windows.

  • PowerShell Core, by contrast, uses BOM-less UTF-8 by default (both for Out-File / > and Set-Content) and offers you a choice of BOM or no-BOM via -Encoding specifiers utf8 and utf8BOM.

[1] From a Microsoft blog post, emphasis added: "Windows PowerShell 5.1, much like .NET Framework 4.x, will continue to be a built-in, supported component of Windows 10 and Windows Server 2016. However, it will likely not receive major feature updates or lower-priority bug fixes." and, in a comment, "The goal with PowerShell Core 6.0 and all the compatibility shims is to supplant the need for Windows PowerShell 6.0 while converging the ecosystem on PowerShell Core. So no, we currently don’t have any plans to do a Windows PowerShell 6.0."

Friday, July 30, 2021
Jeremy Pare
answered 4 Months ago

Your newlines are coming from the print function


import sys

sys.stdout.write ('some stuff')

and your line breaks will go away

Wednesday, August 11, 2021
answered 4 Months ago

you can only do this in HTML5 compliant browsers that implemented the FileReader interface (Firefox, Chrome, IE10, Safari 5.1+, not sure about Opera).

You basically use FileReader to get the contents of your file into memory and then manually scan (in client-side javascript) for the MP3 tags you want.

Tuesday, August 17, 2021
Benoit Wickramarachi
answered 4 Months ago

the files are names of file objects in root directory.

dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

try this

for root, dirs, files in os.walk("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/pcoip-logs"):
    lineContainsServerFile = re.compile('.*server.*')
    for filename in files:
            if lineContainsServerFile.match(filename):
                    filename = os.path.join(root, filename)
                    with open(filename,'rb') as files:
                            print 'filename:', filename
Sunday, August 29, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :