Cannot write CSV file from python - python-3.x

I'm been trying to create a csv file from this code, but it fails every time, I have tried different ways to place it inside the code but nothing has work so far.
I'm new to python and to Stack overflow.
If somebody can explain what I'm doing wrong it will be helpful.
Thanks in advance for any help.
from time import sleep
import os
import sys
from bleson import get_provider, Observer, UUID16
import csv
GOVEE_BT_mac_OUI_PREFIX = "A4:C1:38"
H5075_UPDATE_UUID16 = UUID16(0xEC88)
govee_devices = {}
# ###########################################################################
FORMAT_PRECISION = ".2f"
# Decode H5075 Temperature into degrees Fahrenheit
def decode_temp_in_f(encoded_data):
return format((((encoded_data / 10000) * 1.8) + 32), FORMAT_PRECISION)
# Decode H5075 percent humidity
def decode_humidity(encoded_data):
return format(((encoded_data % 1000) / 10), FORMAT_PRECISION)
#focus here
with open('temp.csv','w',newline='') as record:
record = csv.writer(record)
record.writerow(['Device Name','Device Address','Temp','Humidity'])
def print_values(mac):
govee_device = govee_devices[mac]
print(govee_device['name'],govee_device['address'],govee_device['tempInF'],govee_device['humidity'],govee_device['battery'])
record.writerow(govee_device['name'])
# On BLE advertisement callback
def on_advertisement(advertisement):
if advertisement.address.address.startswith(GOVEE_BT_mac_OUI_PREFIX):
mac = advertisement.address.address
if mac not in govee_devices:
govee_devices[mac] = {}
if H5075_UPDATE_UUID16 in advertisement.uuid16s:
# HACK: Proper decoding is done in bleson > 0.10
name = advertisement.name.split("'")[0]
encoded_data = int(advertisement.mfg_data.hex()[6:12], 16)
battery = int(advertisement.mfg_data.hex()[12:14], 16)
govee_devices[mac]["address"] = mac
govee_devices[mac]["name"] = name
govee_devices[mac]["mfg_data"] = advertisement.mfg_data
govee_devices[mac]["data"] = encoded_data
govee_devices[mac]["tempInF"] = decode_temp_in_f(encoded_data)
govee_devices[mac]["humidity"] = decode_humidity(encoded_data)
govee_devices[mac]["battery"] = battery
print_values(mac)
if advertisement.rssi is not None and advertisement.rssi != 0:
govee_devices[mac]["rssi"] = advertisement.rssi
# ###########################################################################
adapter = get_provider().get_adapter()
observer = Observer(adapter)
observer.on_advertising_data = on_advertisement
try:
while True:
observer.start()
sleep(2)
observer.stop()
except KeyboardInterrupt:
try:
observer.stop()
sys.exit(0)
except SystemExit:
observer.stop()
os._exit(0)
Error that Im getting is:
File "/home/pi/GoveeWatcher-master/python/goveeWatcher.py", line 37, in print_values
record.writerow(govee_device['name'])
ValueError: I/O operation on closed file.

I would be tempted to put the CSV writing functionality inside of the print_values function so it opens the file, writes the data, and then closes the file on each value that is found by the observer.
For example:
#focus here
def print_values(mac):
govee_device = govee_devices[mac]
print(govee_device['name'], govee_device['tempInF'])
with open('temp.csv','a',newline='') as record:
writer = csv.DictWriter(record, fieldnames=govee_device.keys())
writer.writerow(govee_device)

Related

Ubuntu 22.04: pyautogui.locateOnScreen is returning None. How to solve this?

My OS is Ubuntu 22.04, Python 3.10.4.
I am trying to create a code to automate Whatsapp send message.
Have installed latest version of pyautogui.
Following is the code I am running:
import pyautogui as pt
import paperclip as pc
# from pynput.mouse import Controller, Button
from time import sleep
# mouse = Controller()
class WhatsApp:
def __init__(self, speed=5, click_speed=.3):
self.speed = speed
self.click_speed = click_speed
self.message = ''
self.last_message = ''
def nav_green_dot(self):
try:
# position = pt.locateOnScreen('clip_pic.png', confidence = .7)
# position = pt.locateOnScreen('clip_pic.png')
# print(position)
print(pt.locateOnScreen('clip_pic.png'))
# pt.moveTo(position[0:2], duration = self.speed)
# pt.moveRel(-100, 0, duration = self.speed)
except Exception as e:
print ('Exception (nav_green_dot): ', e)
wa_bot = WhatsApp(speed = .5, click_speed = .4)
sleep(5)
wa_bot.nav_green_dot()
At print(pt.locateOnScreen('clip_pic.png')) I am getting None.
Attached is the picture I am trying to capture.
I have already opencv-python installed as well.
I also have whatsapp web page opened in a chrome browser.
I tested in firefox as well.
The output error is not clear in what direction I should go.
What am I missing?
Finding image on screen for only one time may be None you need to check repeatedly for it. And if it is found you can end the loop you are using to find it. You should use python's multithreading for it. here is an updated version of your code
import pyautogui as pt
import paperclip as pc
# from pynput.mouse import Controller, Button
from time import sleep
import threading
# mouse = Controller()
FOUND_IMAGE = False
def checkFunction():
global FOUND_IMAGE
while True:
img = pt.locateOnScreen('img.png')
if img != None:
FOUND_IMAGE = True
break
checkThread = threading.Thread(target=checkFunction) # creating therad
checkThread.start() # starting therad
class WhatsApp:
def __init__(self, speed=5, click_speed=.3):
self.speed = speed
self.click_speed = click_speed
self.message = ''
self.last_message = ''
def nav_green_dot(self):
try:
# position = pt.locateOnScreen('clip_pic.png', confidence = .7)
# position = pt.locateOnScreen('clip_pic.png')
# print(position)
print(FOUND_IMAGE)
# pt.moveTo(position[0:2], duration = self.speed)
# pt.moveRel(-100, 0, duration = self.speed)
except Exception as e:
print ('Exception (nav_green_dot): ', e)
wa_bot = WhatsApp(speed = .5, click_speed = .4)
sleep(5)
wa_bot.nav_green_dot()
For any queries have a look at this question or this Post

How do I create imap checker that reads from csv file and loop for each line using multithreading?

The script only checks the first 10 lines of csv file, I want the script to iterate over all the lines of the file using threads to speed up the process.
Code:
import time
import csv
import imaplib
from threading import Thread
combo = []
FileToOpen = open("emails.csv", "r")
csvDictReader = csv.DictReader(FileToOpen)
successEmail = open("SuccessEmails.txt", "a")
for email in csvDictReader:
combo.append(email)
rows_count = len(list(csvDictReader))
t1 = time.perf_counter()
combo_new = combo
def ConnectorImap(combo_new):
for Email in combo_new:
login = Email['login']
password = Email['password']
imap_serv = "imap." + login.split('#')[-1]
mail_serv = "mail." + login.split('#')[-1]
try:
print(login,password,imap_serv)
print('logging in as %s' % login)
# create an IMAP4 class with SSL
imap_ssl = imaplib.IMAP4_SSL(imap_serv)
resp_code, response = imap_ssl.login(login,password)
print(resp_code)
if resp_code == "OK":
successEmail.write(login + ',' + password)
successEmail.write("\n")
imap_ssl.logout()
except Exception as e:
print(e)
pass
threads = []
for idx, line in enumerate(rows_count):
# We start one thread per url present.
process = Thread(target=ConnectorImap, args=(combo_new))
process.start()
threads.append(process)
for process in threads:
process.join()
t2 = time.perf_counter()
print(f'finished in{t2 - t1} seconds')
I am new to python please any help! I want the script to iterate over all the lines of the file using threads to speed up the process.

Python watchdog module duplicate events (edit: was not an watchdog issue)

I am creating a python script that will identify changes to a log file and print some data from the new logs.
I use watchdog to create an event handler and everything seems to work fine except from that, I get duplicate events every time I modify the file. I checked creation and delete, they both work as expected and trigger one time.
I have read the similar question which explains having a created and a modified event when I save a file but this is not my case. I just get two modification events.
Here is my code:
import os, sys, time
import subprocess
import threading
import win32print
from tkinter import filedialog
from tkinter import *
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class Handler(FileSystemEventHandler):
# docstring for FileSystemEventHandler
def __init__(self, observer, filename, dirname):
# super(Handler, FileSystemEventHandler).__init__(self,)
self.observer = observer
self.filename = filename
self.dirname = dirname
print("Handler filename = " , self.filename)
print("Handler dirname = " , self.dirname)
def on_modified(self, event):
if self.filename == event.src_path:
print("The file was modified")
print (event.src_path)
# go get the last line and print the data
# try:
# hJob = win32print.StartDocPrinter (hPrinter, 1, ("test of raw data", None, "RAW"))
# try:
# win32print.StartPagePrinter (hPrinter)
# win32print.WritePrinter (hPrinter, raw_data)
# win32print.EndPagePrinter (hPrinter)
# finally:
# win32print.EndDocPrinter (hPrinter)
# finally:
# win32print.ClosePrinter (hPrinter)
def on_created(self, event):
print("A file was created (", event.src_path, ")")
def on_deleted(self, event):
print("A file was deleted (", event.src_path, ")")
if __name__ == "__main__":
Flags=2
Name=None
Level=1
printers = win32print.EnumPrinters(Flags, Name, Level)
print("\nChoose a printer to use:")
i=1
for p in printers:
print(i,')' , p[2])
i = i+1
if sys.version_info >= (3,):
raw_data = bytes ("This is a test", "utf-8")
else:
raw_data = "This is a test"
printer = int(input())
printer_name = printers[printer-1][2] #win32print.GetDefaultPrinter ()
print("You chose ", printer_name, "\nI will now print from the specified file with this printer")
hPrinter = win32print.OpenPrinter (printer_name)
# root = Tk()
# root.filename = filedialog.askopenfilename(initialdir = "/Desktop",title = "Select file",filetypes = (("log files","*.log"),("all files","*.*")))
file_path = "some_file_path" # root.filename
file_directory = os.path.dirname(file_path)
# print (file_path)
print (file_directory)
observer = Observer()
event_handler = Handler(observer, file_path, file_directory)
observer.schedule(event_handler, path=file_directory, recursive=False)
observer.start()
observer.join()
any ideas would be appreciated
EDIT:
After some debugging I found out that Windows10 is changing the file modification time twice every time I save it.
The proof of concept code is this:
prev_modification_time = os.path.getmtime(file_path)
while True:
current_mod_time = os.path.getmtime(file_path)
if prev_modification_time != current_mod_time :
print ("the file was modified, last modification time is: ", current_mod_time)
prev_modification_time = current_mod_time
pass
Final edit:
After testing my code on linux (Debian Stretch to be exact) it worked like a charm. So this combined with the previous edit probably shows that watchdog works fine and it is windows10 that has some issue. Should I post it on a different question or here?

how save photo in telegram python bot?

i want to write a telegram bot that save photos .
this is my code , but its not working.
and i don't know what is my problem?
def image_handler(bot, update):
file = bot.getFile(update.message.photo.file_id)
print ("file_id: " + str(update.message.photo.file_id))
file.download('image.jpg')
updater.dispatcher.add_handler(MessageHandler(Filters.photo, image_handler))
updater.start_polling()
updater.idle()
pleas help me to solve my problem.
update.message.photo is an array of photos sizes (PhotoSize objects).
Use file = bot.getFile(update.message.photo[-1].file_id). This will get the image with biggest size available.
Here is my code
from telegram.ext import *
import telegram
def start_command(update, context):
name = update.message.chat.first_name
update.message.reply_text("Hello " + name)
update.message.reply_text("Please share your image")
def image_handler(update, context):
file = update.message.photo[0].file_id
obj = context.bot.get_file(file)
obj.download()
update.message.reply_text("Image received")
def main():
print("Started")
TOKEN = "your-token"
updater = Updater(TOKEN, use_context = True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start_command))
dp.add_handler(MessageHandler(Filters.photo, image_handler))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Unlike the accepted answer suggests, you don't actually need the bot object to get the file:
file = update.message.photo[-1].get_file()
and then download the file:
path = file.download("output.jpg")
to use it for further processing or just have it on your device : )
Here is a vanilla python solution:
import requests
from PIL import Image
# for example, we get the last message
# update = requests.post(f'https://api.telegram.org/bot{TOKEN}/getUpdates').json()['result'][-1]
msg = update['message']
# check whether the message contains a photo
if msg.get('photo', None) == None:
return
# get the photo id with the biggest resolution
file_id = msg['photo'][-1]['file_id']
# get URL by id
file_path = requests.get(f'https://api.telegram.org/bot{TOKEN}/getFile?file_id={file_id}').json()['result']['file_path']
# open URL with Pillow
img = Image.open(requests.get(f'https://api.telegram.org/file/bot{TOKEN}/{file_path}', stream=True).raw)
# save on the disk if needed
img.save('photo.jpg')
#dp.message_handler(commands='start')
9 async def s_photo(message: types.Message):
10 """getting str type of json file"""
11photostr = await bot.get_user_profile_photos(message.from_user.id)
12 """parsing str to json"""
13 photojson = json.loads(photostr.as_json())
14 """giving file unic code to get_file method"""
15 photo = await bot.get_file(photojson['photo'][0][0]['file_id'])
16 """end downloading object with download method"""
17 downloadphoto = await photo.download('filename'+'.jpeg')

object has no attribute 'binary_message'

so I'm doing a project where we use IBM Bluemix's watson to do some speech to text audio stream stuff, but I've kinda hit a wall. There's one call to a method that really messes everything up and I can't figure out why.
import pyaudio
import json
import time
import base64
import threading
import ssl
import numpy as np
from ws4py.client.threadedclient import WebSocketClient
from otherkinds import otherKinds
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 1000
RECORD_SECONDS = 20
WAVE_OUTPUT_FILENAME = "file.wav"
window = np.blackman(CHUNK)
x = 0
audio = pyaudio.PyAudio()
class SpeechToTextClient(WebSocketClient):
def __init__(self):
ws_url = "wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize"
username = "a9cf87f4-7b1b-4c0a-bc7d-f4100a596a08"
password = "zBpZmqivDqAl"
authstring = "{0}:{1}".format(username, password)
base64string = base64.b64encode(authstring.encode('utf-8')).decode('utf-8')
self.listening = False
try:
WebSocketClient.__init__(self, ws_url,
headers=[("Authorization", "Basic %s" % base64string)])
self.connect()
except:
print("Failed to open WebSocket.")
def opened(self):
print("opened")
self.send('{"action": "start", "content-type": "audio/l16;rate=16000"}')
self.stream_audio_thread = threading.Thread(target=self.stream_audio)
self.stream_audio_thread.start()
def received_message(self, mensage):
message = json.loads(str(mensage))
if "state" in message:
if message["state"] == "listening":
self.listening = True
print('Listening...')
otherKinds.main_piece(str(mensage))
def stream_audio(self):
print("Waiting for Watson")
while not self.listening:
time.sleep(0.1)
print("Connected to Watson")
while self.listening:
for i in range(0, int(RATE / CHUNK * 2)):
data = stream.read(CHUNK, exception_on_overflow=False)
try:
self.send(bytearray(data), binary=True)
except ssl.SSLError:
pass
self.send('{"action": "stop"}')
time.sleep(1)
def close(self):
self.listening = False
self.stream_audio_thread.join()
WebSocketClient.close(self)
if __name__ == "__main__":
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True,
frames_per_buffer=CHUNK)
# Here is where we will merge the audio part
stt_client = SpeechToTextClient()
while (1):
pass
stream.stop_stream()
stream.close()
p.terminate()
This is the part that totally goofs everything up:
otherKinds.main_piece(str(mensage))
and this is the error message it gives me:
File "C:\Users\jared\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\jared\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/jared/PycharmProjects/smartAssistant/testreciord.py", line 70, in stream_audio
self.send(bytearray(data), binary=True)
File "C:\Users\jared\Anaconda3\lib\site-packages\ws4py\websocket.py", line 299, in send
message_sender = self.stream.binary_message if binary else self.stream.text_message
AttributeError: 'NoneType' object has no attribute 'binary_message'
I've got no idea what happening here, it works fine if I take the call out, but thats the only thing messing it up. It doesn't matter if the method is in the same file, not in a class or whatever. Any help would be great, thanks! :D

Resources