Python Forum
reading lines from a string [Solved]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading lines from a string [Solved]
#1
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)
Reply


Messages In This Thread
reading lines from a string [Solved] - by ebolisa - Mar-27-2021, 02:55 PM
RE: reading from a file - by deanhystad - Mar-27-2021, 02:58 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 03:01 PM
RE: reading from a file - by deanhystad - Mar-27-2021, 03:06 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 03:09 PM
RE: reading from a file - by deanhystad - Mar-27-2021, 03:37 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 03:37 PM
RE: reading from a file - by deanhystad - Mar-27-2021, 03:54 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 04:02 PM
RE: reading from a file - by ebolisa - Mar-27-2021, 04:35 PM
RE: reading lines from a string - by deanhystad - Mar-27-2021, 04:39 PM
RE: reading lines from a string - by ebolisa - Mar-27-2021, 05:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 482 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 899 Apr-23-2023, 05:27 PM
Last Post: Larz60+
  reading a table which is of type string saisankalpj 2 1,016 Dec-03-2022, 11:19 AM
Last Post: saisankalpj
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,604 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,187 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 1,353 Jul-11-2022, 09:22 AM
Last Post: ibreeden
  Editing text between two string from different lines Paqqno 1 1,364 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,275 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,344 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,356 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020