I write a program that can open a particular file, i.e., a Flash file, positioning it at particular position and closing it(not minimizing but exitting).
The first second target is ok by os.startfile('file.swf'), finding its hwnd by win32gui.FindWindow(None, file.swf) and positioning it by win32gui.MoveWindow(hwnd,0,0,800,600,True), however, the win32gui.DestroyWindow(hwnd) can't work and I don't know why.
Here is the error message below:
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
win32gui.DestroyWindow(hwnd1)
error: (5, 'DestroyWindow', '\xa6s\xa8\xfa\xb3Q\xa9\xda\xa1C')
What is wrong with it? How to fix it?
edit:
my code is:
import win32gui
import os
"""
monitoring a folder which will be updated per one miniute
if the temperature changed the program will open a particular file to display
"""
FLASH_PATH="santa"
PIC_PATH=""
TEMP_PATH="Temperatures/"
file_name="led_20.swf"
filePath=os.path.join(FLASH_PATH,file_name)
os.startfile(filePath)
hwnd=win32gui.FindWindow(None,file_name)
win32gui.MoveWindow(hwnd,0,0,800,600,True)
"""
display it for a few minute and close it
"""
Error 5 is ERROR_ACCESS_DENIED. DestroyWindow() can only be called by the thread that created the window. Post or send a WM_DESTROY message instead.
Related
I have some simple code which opens a word document, updates the fields in it, then closes it. This code works correctly when I manually run it, however when I run it via the SQL Server Agent, I get an error. The user that SQL Server Agent runs with is the same user I am logged in with when running manually. I know the agent can 'see' the document because earlier code used in the task actually creates this document.
Code:
import win32com.client
word = win32com.client.DispatchEx("Word.Application")
filePath = "\\\\network\\data\\file.docx"
doc = word.Documents.Open(filePath)
doc.TablesOfContents(1).Update()
doc.Close(SaveChanges=True)
word.Quit()
Error:
Executed as user: DOMAIN\User. Traceback (most recent call last):
File "\\network\data\file.docx", line 8, in
doc.TablesOfContents(1).Update() AttributeError: 'NoneType' object
has no attribute 'TablesOfContents'
Why might this behavior happen?
For anyone in the future that might get this issue, I fixed it like this:
Open dcomcnfg Navigate to Component Services>Computers>My
Computer>DCOM Config>Microsoft Word 97-2003 Document
Right-click, click Properties
Select the Identity Tab
Select 'The Interactive User'
I have already seen the examples on here of using python's os library to get a local file's time stamp in python by passing it the local path (i.e. /var/www/html/etc.../filename.txt), but when I try to pass getmtime a link, it cannot process it.
Here is what the code looks like:
import os
print(os.path.getmtime('https://www.sec.gov/Archives/edgar/data/1474439/000169655519000022/xslF345X03/wf-form4_156772823294389.xml'))
Here is the error I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python3.7/genericpath.py", line 55, in getmtime
return os.stat(filename).st_mtime
FileNotFoundError: [Errno 2] No such file or directory: 'https://www.sec.gov/Archives/edgar/data/1474439/000169655519000022/xslF345X03/wf-form4_156772823294389.xml'
I know that this link exists.
So it obviously doesn't like me passing it a link. Is there another function that you use to pass links, to get the last modification time of a remote file?
An URL is not necessarily a file. You can ask the remote server to tell you about the link, and the remote server may provide a Last-Modified header, or may not, at the remote server's discretion. It could also lie, if so instructed. In order to do this, you would need to make a HTTP request; the easiest way to do it from Python is the nice requests library.
import requests
import dateutil.parser
response = requests.head(url)
last_modified = response.headers.get('Last-Modified')
if last_modified:
last_modified = dateutil.parser.parse(last_modified)
I tried to make a python program that would allow me to download a jpg file from a website.
Why I'm doing this is really for no reason at all, I just wanted to try it for fun.
Anyways, here is the code:
import urllib
a = 1
while a == 1:
urllib.urlretrieve("http://lemerg.com/data/wallpapers/38/957049.jpg","D:\\Users\\Elias\\Desktop\\FolderName-957049.jpg")
(You may have to properly tab it in, it wouldn't let me here)
So basically what I want it to do is to repeatedly download the same file until I close the program. Just don't ask why.
The error code I get is:
Traceback (most recent call last):
urllib.urlretrieve("http://lemerg.com/data/wallpapers/38/957049.jpg","D:\Users\Elias\Desktop\FolderName-957049.jpg")
AttributeError: 'module' object has no attribute 'urlretrieve'
urlretrieve() in Python3 is in the urllib.request module. Do this:
from urllib import request
a = 1
while a == 1:
request.urlretrieve("http://lemerg.com/data/wallpapers/38/957049.jpg","D:\\Users\\Elias\\Desktop\\FolderName-957049.jpg")
My app is in VB.Net. I have a textbox. The user writes a piece of Python code init. I want to run this code. For example, the code in textbox is something like this:
print 7*7
the result of running this code in Python is 49. But if the user forgets a space and writes:
print7*7
the result of running this code in Python is:
Traceback (most recent call last):
File "vm_main.py", line 33, in <module>
import main
File "/tmp/vmuser_jrlbqyaetu/main.py", line 8, in <module>
print7*7
NameError: name 'print7' is not defined
Now I want to save the result of running code (error or correct data) in a string in VB.Net. Questions:
What is the data type of the result of running the code?
Is it possible to access it?
Is it possible to save it? Is it possible to save it in a string? If yes, how?
You can try this:
import sys
f = open('output.txt', 'w')
sys.stdout = f
###############
#your code here
##############
And than all outputs will be written in ouput?txt
I am writing a Python script to notify me when changes are made to a webpage and store the current state of the page to a file in order to resume seamlessly after rebooting.
The code is as follows:
import urllib
url="http://example.com"
filepath="/path/to/file.txt"
try:
html=open(filepath,"r").read() # Restores imported code from previous session
except:
html="" # Blanks variable on first run of the script
while True:
imported=urllib.urlopen(url)
if imported!=html:
# Alert me
html=imported
open(filepath,"w").write(html)
# Time delay before next iteration
Running the script returns:
Traceback (most recent call last):
File "April_Fools.py", line 20, in <module>
open(filepath,"w").write(html)
TypeError: expected a character buffer object
------------------
(program exited with code: 1)
Press return to continue
I've no idea what this means. I'm relatively new to Python. Any help would be much appreciated.
urllib.urlopen does not return a string, it returns a response as a file-like object. You need to read that response:
html = imported.read()
Only then is html a string you can write to a file.
As an aside, using open(filename).read() is not considered good style, because you never close the file. The same goes for writing. Try using a context manager instead:
try:
with open(filepath,"r") as htmlfile:
html = htmlfile.read()
except:
html=""
The with block will automatically close the file when you leave the block.