[Tutor] problem with back slash

Alex Kleider alexkleider at gmail.com
Wed Feb 23 14:37:59 EST 2022


I've written myself a little utility that accepts a text file which
might have very long lines and returns a file with the same text but
(as much as possible) with the lines no longer than MAX_LEN
characters. (I've chosen 70.)
It seems to work except when the the source file contains back
slashes! (Presence of a back slash appears to cause the program
to go into an endless loop.)
I've tried converting to raw strings but to no avail.

Here's the code, followed by an example source file.

'''
#!/usr/bin/env python3
# file: limit_line_length.py
"""
Accepts a text file and tries to shorten lines to MAX characters.
First parameter must be a file name.
Optional second parameter can be the output file's name.
If 2nd param is not specified, output will go to "new_<1stParam>".
"""

import sys

MAX = 70


def split_on_space_closest_to_max_len(line, max_len=MAX):
    """
    Returns a tuple of two (possibly empty) strings.
    If the line is <= <max_len>: it is returned as t[0] & t[1] as ''.
    If indented beyond <max_len> t[0] as '' & t[1] as line[max_len:]
    If there are no spaces then t[0] as <line> and t[1] as ''.
    If the first space is beyond <max_len>: t[0] is what is up to the
    space and t[1] what was after the space.
    Otherwise t[0] is the longest it can be up to max_len up to a
    space and t[1] is what comes after the space.
    Trailing spaces are stripped.
    """
    line = line.rstrip()
    line_length = len(line)
    if line_length <= max_len:  # simplest scenario
        return (line, '')       # empty lines included
    original_line = line[:]
    unindented_line = line.lstrip()
    n_leading_spaces = line_length - len(unindented_line)
    if n_leading_spaces > max_len:  # big indentation!!!
        return ('', line[max_len:])
    indentation = ' ' * n_leading_spaces
    max_len -= n_leading_spaces
    i_last_space = unindented_line.rfind(' ')
    if i_last_space == -1:  # no spaces on which to split
        return (line, '')
    i_space = unindented_line.find(' ')
    if i_space > max_len:
        return (indentation + unindented_line[:i_space],
                unindented_line[i_space+1])
    while True:
        next_space = unindented_line.find(' ', i_space+1)
        if next_space > max_len: break
        else: i_space =next_space
    return (indentation + unindented_line[:i_space],
            unindented_line[i_space +1:])

args = sys.argv
arglen = len(args)
infile = args[1]
if arglen > 2: dest_file = args[2]
else: dest_file = "new_{}".format(infile)
with open(infile, 'r') as source:
    with open(dest_file, 'w') as dest:
        for line in source:
#           line = repr(line.rstrip())
            print("line: '{}'".format(line))  # for debugging
            while line:
                line2write, line = split_on_space_closest_to_max_len(
                        line)
                print("writing: '{}'".format(line2write))  # for debugging
                print("remaining: '{}'".format(line))  # for debugging
#               dest.write(repr(line2write) + '\n')
                dest.write(line2write + '\n')
'''

Example text file:
"""
https://superuser.com/questions/1313241/install-windows-10-from-an-unbooted-oem-drive-into-virtualbox/1329935#1329935

You can activate Windows 10 using the product key for your hardware which
is embedded in the BIOS in an ACPI table called MSDM (Microsoft Data
Management). You can get it like this (from Linux, of course!):

$ sudo tail -c +56 /sys/firmware/acpi/tables/MSDM
ABA2D-TEFJ4-D97PT-9B42Y-H3U5E

You can apply the OEM Windows license to a VirtualBox guest like this (from
the Linux host - assuming VM is called win10):

$ sudo cat /sys/firmware/acpi/tables/MSDM > ~/VirtualBox\ VMs/win10/msdm.bin
$ VBoxManage setextradata win10 \
               "VBoxInternal/Devices/acpi/0/Config/CustomTable" \
               ~/VirtualBox\ VMs/win10/msdm.bin

With that in place, Windows will not ask for a product key during
installation, it will activate automatically. If you want to verify that it
does indeed use the correct key you can use a tool like ShowKeyPlus to
check it. You can read about ShowKeyPlus on TenForums or download it here.
"""

Thanks in advance for any advice.
PS Running Python 10 within virtualenvwrapper on Debian/11.


More information about the Tutor mailing list