Unable to create file system object in wmi using python - python

I connected to remote windows server using wmi. I want to create filesystem object to extract file version of file on remote server.
My code goes like this:
# mc_name-machine name, login_machine() to login
c = login_machine(mc_name)
print "logged-in"
# erroneous line.
fo = c.win32com.client.Dispatch('Scripting.filesystemobject')
# path=path of file on remote machine
print fo.GetFileVersion(path)
Help will be greatly appreciated.
For the erroneous line in above code error thrown:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\wmi.py", line 1145, in __getattr__
return self._cached_classes (attribute)
File "C:\Python34\lib\site-packages\wmi.py", line 1156, in _cached_classes
self._classes_map[class_name] = _wmi_class (self, self._namespace.Get (class_name))
File "<COMObject <unknown>>", line 3, in Get
File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 282, in _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemServicesEx', 'Not found ', None, 0, -2147217406), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
fo=c.win32com.client.Dispatch('Scripting.filesystemobject')
File "C:\Python34\lib\site-packages\wmi.py", line 1147, in __getattr__
return getattr (self._namespace, attribute)
File "C:\Python34\lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.win32com
I was trying to solve this by using ProvideConstants. Code for same
fo = win32com.client.Dispatch('Scripting.filesystemobject')
foo=ProvideConstants(fo)
I'm not sure how to use this wmi.ProvideConstants object to get file version.

I was able to query the version of a remote file with this code:
c = wmi.WMI('computer', user='domain\\user', password='pass')
result = c.query('SELECT * FROM CIM_DataFile WHERE Name = "C:\\path\to\file"')
for file in result:
print file.Version
Update: To get the LastModifed time as a python datetime object, and print it out in dd/mm/yyyy format:
from datetime import datetime
last_modified = datetime(*wmi.to_time(file.LastModified)[:7])
print last_modified.strftime("%d/%m/%Y")

Related

ChannelAttribution built-in function “markov_model_local_api” not working on sample codes

I am working on a channels attribution project in Python. I use the python library "ChannelAttribution". When I followed the instruction from the official documentation and tried to use the built-in function "markov_model_local_api", it kept throwing an error. Then I tried to run the sample codes from https://channelattribution.io/pdf/ChannelAttributionWhitePaper.pdf:
from ChannelAttribution import *
#Download data
Data = pd.read_csv("https://channelattribution.io/csv/Data.csv",sep=";")
#Generate token
generate_token(email="my_email", job="my_job", company="my_institution")
token = 'my_token'
#Train
res=markov_model_local_api(token, Data, var_path="path", var_conv="total_conversions",var_value="total_conversion_value", var_null="total_null", order=1, sep=">")
It throws these errors below:
Exception ignored in: <function Connection.__del__ at 0x7fba45595670>
Traceback (most recent call last):
File "/Users/li/opt/anaconda3/lib/python3.8/site-packages/pysftp/__init__.py", line 1013, in __del__
self.close()
File "/Users/li/opt/anaconda3/lib/python3.8/site-packages/pysftp/__init__.py", line 784, in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'
Traceback (most recent call last):
File "src/cypack/ChannelAttribution.pyx", line 1188, in ChannelAttribution.markov_model_local_api
File "src/cypack/ChannelAttribution.pyx", line 894, in ChannelAttribution.__f_initialize_connection
File "/Users/li/opt/anaconda3/lib/python3.8/site-packages/pysftp/__init__.py", line 132, in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "/Users/li/opt/anaconda3/lib/python3.8/site-packages/pysftp/__init__.py", line 71, in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
SSHException: No hostkey for host 13.58.174.83 found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/li/Downloads/untitled0.py", line 18, in <module>
res=markov_model_local_api(token, Data, var_path="path", var_conv="total_conversions", var_value="total_conversion_value", var_null="total_null", order=1, sep=">")
File "src/cypack/ChannelAttribution.pyx", line 1248, in ChannelAttribution.markov_model_local_api
UnboundLocalError: local variable 'filename' referenced before assignment
How can I fix all errors and exceptions?

Specified type 'google_ads_failure' does not exist in Google Ads API v8

I have two issues:
I'm calling G-Ads API to bulk remove ads (code)
for error_detail in error_details:
# Retrieve an instance of the google_ads_failure class from the client
failure_message = gAdsServiceWrapper.client.get_type("google_ads_failure")
# Parse the string into a google_ads_failure message instance.
# To access class-only methods on the message we retrieve its type.
google_ads_failure = type(failure_message)
failure_object = google_ads_failure.deserialize(error_detail.value)
for error in failure_object.errors:
# Construct and print a string that details which element in
# the above ad_group_operations list failed (by index number)
# as well as the error message and error code.
print("A partial failure at index "
f"{error.location.field_path_elements[0].index} occurred "
f"\nError message: {error.message}\nError code: "
f"{error.error_code}")
index_array.append(error.location.field_path_elements[0].index)
error_array.append({"error_message": error.message, "error_code": error.error_code})
I get a partial error
And the code fails to parse it (taken form the official website)
My terminal shows:
Partial failures occurred. Details will be shown below.
Traceback (most recent call last):
File "D:\projects\bowling\venv\lib\site-packages\google\ads\googleads\client.py", line 426, in get_type
message_class = getattr(type_classes, name)
File "D:\projects\bowling\venv\lib\site-packages\google\ads\googleads\v8\__init__.py", line 1753, in __getattr__
raise AttributeError(f"unknown type {name!r}.")
AttributeError: unknown type 'google_ads_failure'.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\projects\bowling\src\main.py", line 535, in <module>
main(args.top_id)
File "D:\projects\bowling\src\main.py", line 141, in main
removed_ads_count = remove_disapproved_ads_for_account(account)
File "D:\projects\bowling\src\main.py", line 206, in remove_disapproved_ads_for_account
remove_ads(ad_removal_operations, ads_to_remove_json, account_id)
File "D:\projects\bowling\src\main.py", line 300, in remove_ads
index_array, error_array = _print_results(response_chunk)
File "D:\projects\bowling\src\main.py", line 439, in _print_results
failure_message = gAdsServiceWrapper.client.get_type("google_ads_failure")
File "D:\projects\bowling\venv\lib\site-packages\google\ads\googleads\client.py", line 428, in get_type
raise ValueError(
ValueError: Specified type 'google_ads_failure' does not exist in Google Ads API v8
failure_message = gAdsServiceWrapper.client.get_type("google_ads_failure")
I think this should be -
failure_message = gAdsServiceWrapper.client.get_type("GoogleAdsFailure")

Error coming in this CreateObject from com types?

Well here is my Simple Code:
from comtypes.client import CreateObject
engine = CreateObject("SAPI.SpVoice")
#stream = CreateObject("SAPI.SpFileStream")
And this much errors comes.
Traceback (most recent call last):
File "E:\Python\lib\ctypes_init_.py", line 123, in WINFUNCTYPE
return win_functype_cache[(restype, argtypes, flags)]
KeyError: (<class 'ctypes.HRESULT'>, (<class 'comtypes.automation.tagVARIANT'>, <class 'ctypes.wintypes.LP_c_long'>), 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\Python\cv2_tutorials\Text to Speech\text2speech.py", line 2, in
engine = CreateObject("SAPI.SpVoice")
File "E:\Python\lib\site-packages\comtypes\client_init.py", line 250, in CreateObject
return manage(obj, clsid, interface=interface)
File "E:\Python\lib\site-packages\comtypes\client_init.py", line 188, in manage
obj = GetBestInterface(obj)
File "E:\Python\lib\site-packages\comtypes\client_init.py", line 110, in GetBestInterface
mod = GetModule(tlib)
File "E:\Python\lib\site-packages\comtypes\client_generate.py", line 118, in GetModule
mod = _CreateWrapper(tlib, pathname)
File "E:\Python\lib\site-packages\comtypes\client_generate.py", line 198, in _CreateWrapper mod = my_import(fullname)
File "E:\Python\lib\site-packages\comtypes\client_generate.py", line 19, in my_import
return import(fullname, globals(), locals(), ['DUMMY'])
File "E:\Python\lib\site-packages\comtypes\gen_C866CA3A_32F7_11D2_9602_00C04F8EE628_0_5_4.py", line 438, in
ISpeechBaseStream.methods = [
File "E:\Python\lib\site-packages\comtypes_init.py", line 323, in setattr
self.make_methods(value)
File "E:\Python\lib\site-packages\comtypes_init.py", line 692, in make_methods
prototype = WINFUNCTYPE(restype, *argtypes)
File "E:\Python\lib\ctypes_init.py", line 125, in WINFUNCTYPE
class WinFunctionType(_CFuncPtr):
TypeError: item 1 in argtypes passes a union by value, which is unsupported.
I know one thing that it is very simple mistake but i can't find it.
Well I got the Answer of my Own Question
and the Answer is very Simple :-
**Just Uninstall the pyttsx3 and comtypes : **
pip uninstall pyttsx3
pip uninstall comtypes
**and Then Reinstall Them by typing this : **
pip install comtypes
pip install -Iv pyttsx3==2.6 -U
It'll solve the problem ,thank me later

twitterImgBot stops working after some hours

I'm trying to get this, https://github.com/joaquinlpereyra/twitterImgBot, to work
and it works and it seems ok.
But after some hours, it stops working and this error comes up:
*python3 twitterbot.py
Traceback (most recent call last):
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 118, in build_path
value = quote(self.session.params[name])
KeyError: 'id'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "twitterbot.py", line 209, in <module>
main()
File "twitterbot.py", line 200, in main
orders()
File "twitterbot.py", line 118, in orders
timeline.delete_tweet_by_id(tweet.in_reply_to_status_id, api)
File "/home/user/Skrivebord/twitterboot/lo/bot/timeline.py", line 12, in delete_tweet_by_id
api.destroy_status(id_to_delete)
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 245, in _call
method = APIMethod(args, kwargs)
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 71, in __init__
self.build_path()
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 120, in build_path
raise TweepError('No parameter value found for path variable: %s' % name)
tweepy.error.TweepError: No parameter value found for path variable: id*
It seems like the Python has some problem because if I make a new install on a another PC it works for some hours and then stops.
Strange.
This is likely because tweet is not in reply to a status, so has an in_reply_to_status_id attribute that's None, so API.destroy_status is called with an id of None.

How to call COM-method using comtypes?

I'm using Python and comtypes (both new to me) to call a COM method (with no arguments), however I get an "exception: access violation reading". When using win32com it works.
import win32com.client
import pywintypes
result=win32com.client.Dispatch("{9275351B-0F06-366F-A560-933FED81FECF}")
result.AutomationMyType()
Result:
('text1', 'text2',...,'text99')
When using comtypes it doesn't work, "exception: access violation reading" is returned.
import comtypes
import comtypes.client as cc
tlb_id = comtypes.GUID("{40B0E4AB-2B70-4142-91A3-839CD72EA140}")
cc.GetModule((tlb_id, 1, 0))
obj=cc.CreateObject("ActLifeVBA.VBA.AutomationAktuar")
obj.AutomationMyType()
Result:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python37\lib\site-packages\comtypes\safearray.py", line 223, in __ctypes_from_outparam__
return self[0]
File "C:\Program Files\Python37\lib\site-packages\comtypes\safearray.py", line 214, in __getitem__
return self.unpack()
File "C:\Program Files\Python37\lib\site-packages\comtypes\safearray.py", line 260, in unpack
for d in range(1, dim+1)]
File "C:\Program Files\Python37\lib\site-packages\comtypes\safearray.py", line 260, in <listcomp>
for d in range(1, dim+1)]
File "C:\Program Files\Python37\lib\site-packages\comtypes\_safearray.py", line 97, in SafeArrayGetLBound
_SafeArrayGetLBound(pa, dim, result)
OSError: exception: access violation reading 0x0000020C5073FFFC

Categories