Python Forum

Full Version: reading lines from a string [Solved]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,

The code below works, but it has too many lines. I tried to shorten it, but I get OSError: [Errno 36] File name too long.

What am I doing wrong?
TIA

#!/usr/bin/python3
from subprocess import check_output
import os

filename = 'tmp.txt'

# get text and save to file
def readFile():
    global filename

    text = check_output('dmesg -T', shell = True).decode('utf-8')

    #write to a file
    file = open(filename,'w')
    file.write(text)
    file.close()


def analyzeFile():
    readFile()
    count = 0

    # read text from file
    with open(filename, 'r') as openfile:
        for line in openfile:
            for part in line.split():
                if 'Bluetooth:' in part:
                    count += 1
    if count > 0:
        emailme(count)


def emailme(txt):
#     print(txt)
    os.remove(filename)
    pass


analyzeFile()
short version:

count = 0
text = check_output('dmesg -T', shell = True).decode('utf-8')
with open(text, 'r') as openfile:
        for line in openfile:
            for part in line.split():
                if 'Bluetooth:' in part:
                    count += 1
print(count)
Is text a filename? And your code makes no sense. Why does "readFile" write a file?. In your short version why is there a file at all? Isn't "text" the text you want to process?
(Mar-27-2021, 02:58 PM)deanhystad Wrote: [ -> ]Is text a filename?

If you mean the one on line 2 of the short version, not really. Is memory which is holding captured text.
Then why are you using it as a filename?
(Mar-27-2021, 03:06 PM)deanhystad Wrote: [ -> ]Then why are you using it as a filename?
Sad how would I treat it?
In your original code you wrte "text" to a file, then you open the file and read the contents. In the short version you try to open a file using a filename that is not a filename. Why are you opening a file?

What is "text"? "It is memory" is not an answer. Everything in a program is memory. Have you tried printing it to see what it contains? Have you asked for it's type? Know the type and the contents and "how would I treat it" will be obvious.
(Mar-27-2021, 03:35 PM)deanhystad Wrote: [ -> ]In your original code you wrte "text" to a file, then you open the file and read the contents. In the short version you try to open a file using a filename that is not a filename. Why are you opening a file?

What is "text"? "It is memory" is not an answer. Everything in a program is memory. What is the type of "text"? Know that and you'll know how to use it.

"text" is a variable filled with several lines obtained executing the command "dmesg -T" in Linux.
No. text is a string. It does not contain lines. It contains newline characters.

In the old code you wrote it to a file, read it back, and counted the number of times 'Bluetooth:' appears in the file. If the count was greater than zero, you called a function.

Can you count the number of times 'Bluetooth:' appears in a string?
(Mar-27-2021, 03:54 PM)deanhystad Wrote: [ -> ]No. text is a string. It does not contain lines. It contains newline characters.

In the old code you wrote it to a file, read it back, and counted the number of times 'Bluetooth:' appears in the file. If the count was greater than zero, you called a function.

Can you count the number of times 'Bluetooth:' appears in a string?

If you mean some like this, it doesn't work:

count = 0

text = check_output('dmesg -T', shell = True).decode('utf-8')

nlines = text.count('\n')

for i in range(nlines):
    if text.find('Bluetooth:'):
        count += 1
print(count)
(Mar-27-2021, 04:02 PM)ebolisa Wrote: [ -> ]
(Mar-27-2021, 03:54 PM)deanhystad Wrote: [ -> ]No. text is a string. It does not contain lines. It contains newline characters.

In the old code you wrote it to a file, read it back, and counted the number of times 'Bluetooth:' appears in the file. If the count was greater than zero, you called a function.

Can you count the number of times 'Bluetooth:' appears in a string?

If you mean some like this, it doesn't work:

count = 0

text = check_output('dmesg -T', shell = True).decode('utf-8')

nlines = text.count('\n')

for i in range(nlines):
    if text.find('Bluetooth:'):
        count += 1
print(count)
but this does and not sure if it's the way to go:

import io

count = 0

text = check_output('dmesg -T', shell = True).decode('utf-8')
buf = io.StringIO(text)

for i in range(text.count('\n')):
    line = buf.readline()
    for part in line.split():
        if 'Bluetooth:' in part:
            count += 1

print(count)
Pages: 1 2