I'm using Winpython on Windows 10 and notepad++, and want to run the following Python code:
import wave, struct, math
sampleRate = 44100.0 # hertz
duration = 1.0 # seconds
frequency = 440.0 # hertz
wavef = wave.open('sound.wav','w')
wavef.setnchannels(1) # mono
wavef.setsampwidth(2)
wavef.setframerate(sampleRate)
for i in range(int(duration * sampleRate)):
value = int(32767.0*math.cos(frequency*math.pi*float(i)/float(sampleRate)))
data = struct.pack('<h', value)
wavef.writeframesraw( data )
wavef.writeframes('')
wavef.close()
However I get the following error:
Traceback (most recent call last):
File "C:\Users\HP\Desktop\WinPython-64bit-2.7.10.3\robin\sound\wav_00.py", line 7, in <module>
wavef = wave.open('sound.wav','w')
File "C:\Users\HP\Desktop\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\lib\wave.py", line 513, in open
return Wave_write(f)
File "C:\Users\HP\Desktop\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\lib\wave.py", line 308, in __init__
f = __builtin__.open(f, 'wb')
IOError: [Errno 13] Permission denied: 'sound.wav'
I'm running the script using C:\Users\HP\Desktop\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\python.exe -i "$(FULL_CURRENT_PATH)"
When I run the script outside of the Winpython folder with my registered Python installation it works fine, but I want to make use of packages that are installed in the Winpython version, so this solution is not adequate
Please could someone explain how to make this work?
Try running as administrator.
If you running from the windows command prompt:
Right click windows button
Select "Command Prompt(Admin)"
Execute the script
Hope this helps
Related
I recently re-activated a python project from last year. It still worked. I recently bought a new computer and upgraded to Windows 11. I also updated my Pycharm to the latest and updated to python 10.1. The program no longer works. Which update is the culprit?
Below shows the essence of the code which does not work. I can successfully download .jpg files but not .pdf files.
from wand.image import Image
file1 = 'U:/Image Files/Bonnie Dundee.jpg'
print(file1)
img1 = Image(filename=file1, resolution=300)
file2 = 'U:/Image Files/Frosty Morning.pdf'
print(file2)
img2 = Image(filename=file2, resolution=300)
The error trace is:
C:\Users\jradc\PycharmProjects\Scratch\venv\Scripts\python.exe C:/Users/jradc/PycharmProjects/Scratch/main.py
U:/Image Files/Bonnie Dundee.jpg
U:/Image Files/Frosty Morning.pdf
Traceback (most recent call last):
File "C:\Users\jradc\PycharmProjects\Scratch\main.py", line 7, in <module>
img2 = Image(filename=file2, resolution=300)
File "C:\Users\jradc\PycharmProjects\Scratch\venv\lib\site-packages\wand\image.py", line 9144, in __init__
self.read(filename=filename)
File "C:\Users\jradc\PycharmProjects\Scratch\venv\lib\site-packages\wand\image.py", line 9815, in read
self.raise_exception()
File "C:\Users\jradc\PycharmProjects\Scratch\venv\lib\site-packages\wand\resource.py", line 222, in raise_exception
raise e
wand.exceptions.DelegateError: FailedToExecuteCommand `"gswin64c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r300x300" -dPrinted=false "-sOutputFile=C:/Users/jradc/AppData/Local/Temp/magick-xx4FiylIV-id96p7igp2OZkQb3jLaJ_t%d" "-fC:/Users/jradc/AppData/Local/Temp/magick-wcsbcSOqVJ1dFFk0CAU_gvAVIYrZi1Ut" "-fC:/Users/jradc/AppData/Local/Temp/magick-1GWjBArMZle6Xrv-HTjtyKzCqE1Csf8H"' (The system cannot find the file specified.
) # error/delegate.c/ExternalDelegateCommand/516
Process finished with exit code 1
Could anyone point to what I need to change to get this back to working?
I corrected the file extension.
Looks like the system could not find gswin64c.exe.
py-Wand uses that to convert PDF to PNG.
Check that ghostscript (you probably want the AGPL release) is installed and in your Path.
In the "explorer", search for gswin64c.exe and note the directory where it is.
Then look for "Environment variables" in the start menu and verify that the aforementioned directory is in the Path. If not, edit the path and add it.
So I am trying to make a download tool to download stuff via a link here is a snippet from my code
if download_choice == "14":
print("Downloading test file to Desktop...")
myfile14 = requests.get(url14)
open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content)
I want to make a program that can download stuff via a link. I want it to run on all PCs not only on mine. Here is the error:
Traceback (most recent call last): File "C:\Users\David\Desktop\Windows Download Tool\Python\WDT.py", line 100, in <module> open('c:/users/%userprofile%/desktop/', 'wb').write(myfile14.content) FileNotFoundError: [Errno 2] No such file or directory: 'c:/users/%userprofile%/desktop/'
Python does not use %userprofile% to get the username of the executing user.
To achieve this, you need to import the package os and run it's getlogin function. This returns a string with the current username
Example:
import os
username = os.getlogin()
if download_choice == "14":
print("Downloading test file to Desktop...")
myfile14 = requests.get(url14)
open(f'C:/Users/{username}/desktop/', 'wb').write(myfile14.content)
I am using an f-string for opening the file, since it is preferred by PEP-8 (it's just correct code-styling)
Attention: This only works on a windows machine
I have installed PyExifTool (https://smarnach.github.io/pyexiftool/). The installation was successful. However, when I try to run the example code provided there:
import exiftool
files = ["test.jpg"]
with exiftool.ExifTool() as et:
metadata = et.get_metadata_batch(files)
for d in metadata:
print("{:20.20} {:20.20}".format(d["SourceFile"],
d["EXIF:DateTimeOriginal"]))
I am getting this error:
Traceback (most recent call last):
File "extract_metadata_03.py", line 5, in <module>
metadata = et.get_metadata_batch(files)
File "c:\Python38\lib\site-packages\exiftool.py", line 264, in get_metadata_batch
return self.execute_json(*filenames)
File "c:\Python38\lib\site-packages\exiftool.py", line 256, in execute_json
return json.loads(self.execute(b"-j", *params).decode("utf-8"))
File "c:\Python38\lib\site-packages\exiftool.py", line 227, in execute
inputready,outputready,exceptready = select.select([fd],[],[])
OSError: [WinError 10093] Either the application has not called WSAStartup, or WSAStartup failed
I have tried with exiftool.exe Version 11.91 stand-alone Windows executable (from https://exiftool.org/) in my path as well as installing exiftool using Oliver Betz's ExifTool Windows installer (https://oliverbetz.de/pages/Artikel/ExifTool-for-Windows)
I have tried two separate Python installations (Python 3.8 and also Python 2.7) with the same behaviour.
Any assistance with this or suggestions for troubleshooting would be greatly appreciated.
You get the error because the select.select() used in exiftool.py is not compatible with Windows. To solve this you can manually add the following to exiftool.py:
if sys.platform == 'win32':
# windows does not support select() for anything except sockets
# https://docs.python.org/3.7/library/select.html
output += os.read(fd, block_size)
else:
# this does NOT work on windows... and it may not work on other systems... in that case, put more things to use the original code above
inputready,outputready,exceptready = select.select([fd],[],[])
for i in inputready:
if i == fd:
output += os.read(fd, block_size)
Source: https://github.com/sylikc/pyexiftool/commit/03a8595a2eafc61ac21deaa1cf5e109c6469b17c
I'm having a problem that I can't excel file.
I was using swapy+pywinauto.
program export excel file with different name (ex. time..)
I used swapy to close the export excel.
from pywinauto.application import Application
app = Application().Start(cmd_line=u'"C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE" \\dde')
xlmain = app.XLMAIN
xlmain.Wait('ready')
xlmain.Close()
app.Kill_()
but got error below.
Traceback (most recent call last):
File "D:/23007.py", line 54, in <module>
xlmain.Wait('ready')
WaitUntil(timeout, retry_interval, lambda: self.__check_all_conditions(check_method_names))
File "C:\Python35\lib\site-packages\pywinauto\timings.py", line 308, in WaitUntil
raise err
pywinauto.timings.TimeoutError: timed out
Process finished with exit code 1
Why do you use app.XLMAIN? Is the title of window similar to XLMAIN? Usually the title is <file name> - Excel so that pywinauto can handle it so: xlmain = app["<file name> - Excel"].
Obviously Wait('ready') raised an exception because the window with title "XLMAIN" or similar is not found.
Generally I would recommend using pyWin32 standard module win32com.client to work with Excel (through standard COM interface). See the second answer here for example: Driving Excel from Python in Windows
I have written the following really simple python script to change the desktop wallpaper on my mac (based on this thread):
from appscript import app, mactypes
import sys
fileName = sys.argv[1:]
app('Finder').desktop_picture.set(mactypes.File(fileName))
However when I run it I get the following output:
Traceback (most recent call last):
File "../Source/SetWallPaper2.py",
line 6, in
app('Finder').desktop_picture.set(mactypes.File(fileName))
File
"/Library/Python/2.5/site-packages/appscript-0.19.0-py2.5-macosx-10.5-i386.egg/appscript/reference.py", line 513, in call
appscript.reference.CommandError:
Command failed: OSERROR: -10000
MESSAGE: Apple event handler failed.
COMMAND:
app(u'/System/Library/CoreServices/Finder.app').desktop_picture.set(mactypes.File(u"/Users/Daniel/Pictures/['test.jpg']"))
I've done some web searching but I can't find anything to help me figure out what OSERROR -10000 means or how to resolve the issue.
fileName = sys.argv[1]
instead of
fileName = sys.argv[1:]
mactypes.File(u"/Users/Daniel/Pictures/['test.jpg']")
See the square brackets and quotes around the filename?