my import os is working properly but while using playsound module I am getting an error - python-3.9

import os
os.startfile('D:\\python\\codewithharry\\chapter1\\song.mp3')
from playsound import playsound
playsound('D:\\python\\codewithharry\\chapter1\\song.mp3')'''
raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
Error 305 for command:
open "D:\python\codewithharry\chapter1\song.mp3"
Cannot specify extra characters after a string enclosed in quotation marks.

I think maybe you have 3 extra single quotations marks at the end of this line:
-playsound('D:\\python\\codewithharry\\chapter1\\song.mp3')''' which should
read:
-playsound('D:\\python\\codewithharry\\chapter1\\song.mp3')

Related

I can't import image in tkinter

enter image description here
I want to use the path to open the image in tkitner
I tried to enter the path, but it always show error(in windows pc)
I tried the same method in my Mac and it worked
thanks
In Python the backslash (\) escapes the main use of a character or symbol.
For instance, if you have a string 'Here\'s an string', the backslash tells Python the quote symbol (') is not the end of the string, but instead a part of the string, so it prints Here's a string.
Using two backslashes (\\) tells Python the backslash is not escaping a character or symbol, but is just a backslash in the file path. Or, like acw1668 said, you can use a forward slash (/).
image=PhotoImage(file="C:\\Users\\User\\Desktop\\example.png")

Using subprocess.call from python that contains escaped characters

am triggering SQL loader from a python script (2.7);
The password does contain an # sign. If I call sql loader from the command line and escape the password (username/\"p#ssword\"#database) the process works. However, when I apply what I believe is the same logic within a python script I get an error:
SQL*Loader-704: Internal error: ulconnect: OCIServerAttach [0]
ORA-12154: TNS:could not resolve the connect identifier specified
Since I can run the same command in the cmd prompt successfully, I don't believe this is an issue with the TNSNAMES.ORA file containing any incorrect or missing parameters. I'm pretty confident this is an issue with calling SQL loader from the subprocess command and the escape characters.
Python Logic:
subprocess.call("sqlldr userid=" +config.ddw["user"] + "/\"" +
config.ddw["password"] +"\"#" + config.ddw["connection"] + "
control=C:/projects/controlFile.ctl log=C:/logFile.log)
If I print this statement the string looks like:
sqlldr userid=USERNAME/"p#ssw0rd"#connection/db
(2.7)control=C:/projects/controlFile.ctl log=C:/logFile.log
When I load the string directly in the command line it works:
sqlldr userid=USERNAME/\"p#ssw0rd\"#connection/db
control=C:/projects/controlFile.ctl log=C:/logFile.log
You need those double-quotes escaped so sqlldr sees them. I don't know python, but it appears you need to change that code to make sure you get a backslash in front of the double-quotes. You may need to escape the backslash too since it is most likely a special character.
Perhaps something like this?
subprocess.call("sqlldr userid=" +config.ddw["user"] + "/\\"" +
config.ddw["password"] +"\\"#" + config.ddw["connection"] + "
This is a SWAG so your mileage may vary a little :-)

inserting image into label

I am told that I can add images to a label, but when I run the following code I get an error message:
unicode error: unicodeescape codec can't decode bytes in position 2-3: truncated \UXXXXXX escape
My code is as simple as possible
from tkinter import *
root = Tk()
x = PhotoImage(file="C:\Users\user\Pictures\bee.gif")
w1 = Label(root, image=x).pack()
root.mainloop()
All the examples I've seen don't include the file path to the image but in that case Python can't find the image.
What am I doing wrong ??
Python is treating \Users as a unicode character because of the leading \U. Since it's an invalid unicode character, you get the error.
You can either use forward slashes ("C:/Users/user/Pictures/bee.gif"), a raw string (r"C:\Users\user\Pictures\bee.gif"), or escape the backslashes ("C:\\Users\\user\\Pictures\\bee.gif")

Robot: Backslash in EXECDIR, Windows path

Underlying problem: I want to enable running robot tests using selenium 2 in a portable Firefox that's stored within EXECDIR.
${firefox_binary}= Evaluate sys.modules['selenium.webdriver.firefox.firefox_binary'].FirefoxBinary('${EXECDIR}${/}Firefox${/}App${/}Firefox${/}Firefox.exe') sys, selenium.webdriver
${firefox_profile}= Evaluate sys.modules['selenium.webdriver.firefox.firefox_profile'].FirefoxProfile('${EXECDIR}${/}Lib${/}SeleniumFirefoxProfile') sys, selenium.webdriver
Create Webdriver Firefox firefox_binary=${firefox_binary} firefox_profile=${firefox_profile}
That works fine if, instead of ${EXECDIR}, I use the actual path.
EXECDIR is something like C:\Users\bart.simpson\workspace\projectname here. The issue is that a backslash, when followed by the b, is converted to the ASCII backslash character. The test log then says:
Evaluating expression 'sys.modules['selenium.webdriver.firefox.firefox_profile'].FirefoxProfile('C:\Users\bart.simpson\workspace\projectname\Lib\SeleniumFirefoxProfile')' failed: OSError: [Errno 20047] Unknown error: 20047: 'C:\\Users\x08art.simpson\\workspace\\projectname\\Lib\\SeleniumFirefoxProfile'
Of course I've tried using ${fixedExecDir}= Replace String ${EXECDIR} '\' '/' and such, but nothing ever changes the outcome.
Ideas? Thanks.
Try treating the path as a raw string literal, by putting an "r" before the quote immediately before ${EXECDIR}:
${firefox_binary}= Evaluate ....FirefoxBinary(r'${EXECDIR}${/}Firefox...')
This should work because the robot variables are substituted before the string is passed to python, so the python interpreter only ever sees the complete string.
If you are unfamiliar with python raw string literals, see this question:
What exactly do “u” and “r” string flags do in Python, and what are raw string literals?

python TypeError: must be encoded string without NULL bytes, not str

Trying to get familiar with python's standard library and doing some mucking around with it on my Windows machine. Using python 2.7 I have the following little script which is intended to look in a directory and rename all of the files therein after removing numerals from the file name. I'm getting a typeerror that says "must be encoded string without NULL bytes, not str"
it calls out lines 5 and 18, noted below, where im using os.path.exists.
Any help would be greatly appreciated!
import os, re, string, glob
path = os.path.normpath('C:\Users\me\Photo Projects\Project Name\Project Photos\Modified\0-PyTest')
ln5:if os.path.exists(path):
print "path exists at " + path
for file in glob.glob(os.path.join(path, '*.jpg')):
new_path = os.path.join(os.path.dirname(file), re.sub('\d', '', os.path.basename(file)))
line18: if not os.path.exists(new_path):
os.rename(file, new_path)
"...Photos\Modified\0-PyTest"
Its taking the \0 as a null character. You have to escape \ using \\, or just put an r before the string to make it raw:
r'C:\Users\me\Photo Projects\Project Name\Project Photos\Modified\0-PyTest'
turns out to be the single backslash problem.
i thought os.path.normpath would format the path as required by the os.
If you are giving a path url just add r before it :
(r'E:\Images\1.png')

Resources