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
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)