Posts: 183 Threads: 71 Joined: Oct 2018 Mar-27-2021, 02:55 PM (This post was last modified: Mar-27-2021, 05:29 PM by ebolisa. Edit Reason: Edited subject ) 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) Posts: 6,920 Threads: 22 Joined: Feb 2020 Mar-27-2021, 02:58 PM (This post was last modified: Mar-27-2021, 03:05 PM by deanhystad.) 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? Posts: 183 Threads: 71 Joined: Oct 2018 (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. Posts: 6,920 Threads: 22 Joined: Feb 2020 Then why are you using it as a filename? Posts: 183 Threads: 71 Joined: Oct 2018 (Mar-27-2021, 03:06 PM)deanhystad Wrote: Then why are you using it as a filename?  how would I treat it? Posts: 6,920 Threads: 22 Joined: Feb 2020 Mar-27-2021, 03:37 PM (This post was last modified: Mar-27-2021, 03:38 PM by deanhystad.) 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. Posts: 183 Threads: 71 Joined: Oct 2018 (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. Posts: 6,920 Threads: 22 Joined: Feb 2020 Mar-27-2021, 03:54 PM (This post was last modified: Mar-27-2021, 03:55 PM by deanhystad.) 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? Posts: 183 Threads: 71 Joined: Oct 2018 Mar-27-2021, 04:02 PM (This post was last modified: Mar-27-2021, 04:07 PM by ebolisa.) (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) Posts: 183 Threads: 71 Joined: Oct 2018 (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) |