I'm trying to launch an executable from python subprocess and I have a problem with the path.
I'm trying to run newman.cmd which is located in
C:\Users\<myself>\AppData\Roaming\npm
I try
subprocess.run("C:\Users\<myself>AppData\Roaming\npm\newman.cmd run","shell=True")
which fails, indicating:
Syntax Error: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape: C:\knxnet\test_differentiel.py, line 71, pos 23
subprocess.run("C:\Users\<myself>\AppData\Roaming\npm\newman.cmd run","shell=True")
If I copy newman.cmd and the appropriate folders in my local path, I end up launching the command as expected, ie. this works:
subprocess.run("newman.cmd -h")
I checked the path and it gives me the following:
ENV = <irrelevant info> ;C:\Users\<myself>\AppData\Roaming\npm
so I though it would find it ? how can I do to avoid copying executables in my current directory ?
thanks for any help ...
Well, I found out the answer thanks to SO ... "Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3
It is interpreted as unicode followed by an unwanted character ... the position indicated in the error was the quote's position, so I didn't catch the hint.
so I tried with r:
subprocess.run(r"C:\Users\a.joly\AppData\Roaming\npm\newman.cmd -h")
and it works. It also works if I double the "\".
thanks for your time ...
Related
While learning the os module in Python and I've come across a problem.
Let's pretend my current working directory is: C:\Users\Москва\Desktop\Coding\Project 1.
I'd like to change the cwd to Desktop but since the path contains some Russian letters (Москва) it throws a Syntax error:
print(os.getcwd()) # C:\Users\Москва\Desktop\Coding\Project 1
os.chdir('C:\Users\Москва\Desktop')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes...
How shall I usually treat non-standard characters in paths and change the directory in my case?
It isn't about the russian, it's about the backslash with u: \U.
When you print os.getcwd, escaped backslashes goes away:
os.getcwd()
# 'C:\\Users\\chris\\Documents\\Москва\\test'
print(os.getcwd())
C:\Users\chris\Documents\Москва\test
And now if you try to use the printed one by copy-paste, python will understand \Users part as a unicode but of course fail. You can simply reproduce by executing
"\Uaaaa"
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape
You can either use raw string, or use escaped backslashes:
os.chdir(r'C:\Users\sjysk\Documents\Москва')
# ^ note `r` here
os.getcwd()
# 'C:\\Users\\chris\\Documents\\Москва'
os.chdir('C:\\Users\\sjysk\\Documents\\Москва')
os.getcwd()
# 'C:\\Users\\chris\\Documents\\Москва'
I'm very new to Python, so I apologize in advance for my stupid questions.
I'm trying to run this app
This is the Python code:
def load_json(path):
with open(path, "r") as json_file:
data = json.load(json_file)
validate(instance=data, schema=schema)
return data
I followed all the steps and think I did everything right, but once I run the app through terminal with required info (Client ID, client Secret and path to Json metadata), I get this error:
"<C:\Users\Administrator\Desktop\spotiminder-master\playlist.json> is not readable."
I tried to change the location of the JSON file and rename the file but neither worked. What am I doing wrong? Thank you.
You're trying to find your file in "<C:\Users\Administrator\Desktop\spotiminder-master\playlist.json>"
But i think, you're file is here: "C:\Users\Administrator\Desktop\spotiminder-master\playlist.json"
remove the <> of your path and it'll work
Watch what happens when I try to use the path for the file you used in an IPython session:
In [1]: path = "C:\Users\Administrator\Desktop\spotiminder-master\playlist.json"
File "<ipython-input-1-1980786d97e2>", line 1
path = "C:\Users\Administrator\Desktop\spotiminder-master\playlist.json"
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
In Python string the \U has special meaning. It is the start of a Unicode escape sequence.
Try using the following filename (note the double backlash): C:\\Users\Administrator\Desktop\spotiminder-master\playlist.json
I'm new to coding and have started to try out the OS module, it occasionally it will work on specific paths.
example:
but when I try to interact with an individual file this will happen:
print(os.stat('my_file.txt'))
>>>filenotfounderror: [errno 2] no such file or directory found.
'my_file.txt'
or when I try to interact with a path that is not in my cwd then this would happen:
print(os.listdir(C:\folder\folder\folder))
>>>SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
position 2-3: truncated \UXXXXXXXX escape
I don't understand why this is happening and it would be great if someone could explain why this is happening, thanks.
Python tells your that my_file.txt does not exist in the current directory in your first example.
Verify that you have a file called my_file.txt and then check the current working directory of your python process with os.getcwd().
For your second example, in python the backslash \ is a special character for escape sequences in a string. For example the linefeed \n or the tab \t.
The error in your example is most likely the result of accidentaly forming an invalid escape sequence by not escaping the backslash itself like this:
print(os.listdir('C:\\folder\\folder\\folder'))
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
I am using Python 3.1 on a Windows 7 machine. Russian is the default system language, and utf-8 is the default encoding.
Looking at the answer to a previous question, I have attempting using the "codecs" module to give me a little luck. Here's a few examples:
>>> g = codecs.open("C:\Users\Eric\Desktop\beeline.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#39>, line 1)
>>> g = codecs.open("C:\Users\Eric\Desktop\Site.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#40>, line 1)
>>> g = codecs.open("C:\Python31\Notes.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 11-12: malformed \N character escape (<pyshell#41>, line 1)
>>> g = codecs.open("C:\Users\Eric\Desktop\Site.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#44>, line 1)
My last idea was, I thought it might have been the fact that Windows "translates" a few folders, such as the "users" folder, into Russian (though typing "users" is still the correct path), so I tried it in the Python31 folder. Still, no luck. Any ideas?
The problem is with the string
"C:\Users\Eric\Desktop\beeline.txt"
Here, \U in "C:\Users... starts an eight-character Unicode escape, such as \U00014321. In your code, the escape is followed by the character 's', which is invalid.
You either need to duplicate all backslashes:
"C:\\Users\\Eric\\Desktop\\beeline.txt"
Or prefix the string with r (to produce a raw string):
r"C:\Users\Eric\Desktop\beeline.txt"
Typical error on Windows because the default user directory is C:\user\<your_user>, so when you want to pass this path as a string argument into a Python function, you get a Unicode error, just because the \u is a Unicode escape. If the next 8 characters after the \u are not numeric this produces an error.
To solve it, just double the backslashes: C:\\user\\<\your_user>...
This will ensure that Python treats the single backslashes as single backslashes.
Prefixing with 'r' works very well, but it needs to be in the correct syntax. For example:
passwordFile = open(r'''C:\Users\Bob\SecretPasswordFile.txt''')
No need for \\ here - maintains readability and works well.
With Python 3 I had this problem:
self.path = 'T:\PythonScripts\Projects\Utilities'
produced this error:
self.path = 'T:\PythonScripts\Projects\Utilities'
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
position 25-26: truncated \UXXXXXXXX escape
the fix that worked is:
self.path = r'T:\PythonScripts\Projects\Utilities'
It seems the '\U' was producing an error and the 'r' preceding the string turns off the eight-character Unicode escape (for a raw string) which was failing. (This is a bit of an over-simplification, but it works if you don't care about unicode)
Hope this helps someone
Or you could replace '\' with '/' in the path.
path = pd.read_csv(**'C:\Users\mravi\Desktop\filename'**)
The error is because of the path that is mentioned
Add 'r' before the path
path = pd.read_csv(**r'C:\Users\mravi\Desktop\filename'**)
This would work fine.
I had this same error in python 3.2.
I have script for email sending and:
csv.reader(open('work_dir\uslugi1.csv', newline='', encoding='utf-8'))
when I remove first char in file uslugi1.csv works fine.
Refer to openpyxl document, you can do changes as followings.
from openpyxl import Workbook
from openpyxl.drawing.image import Image
wb = Workbook()
ws = wb.active
ws['A1'] = 'Insert a xxx.PNG'
# Reload an image
img = Image(**r**'x:\xxx\xxx\xxx.png')
# Insert to worksheet and anchor next to cells
ws.add_image(img, 'A2')
wb.save(**r**'x:\xxx\xxx.xlsx')
I had same error, just uninstalled and installed again the numpy package, that worked!
I had this error.
I have a main python script which calls in functions from another, 2nd, python script.
At the end of the first script I had a comment block designated with ''' '''.
I was getting this error because of this commenting code block.
I repeated the error multiple times once I found it to ensure this was the error, & it was.
I am still unsure why.
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
I am using Python 3.1 on a Windows 7 machine. Russian is the default system language, and utf-8 is the default encoding.
Looking at the answer to a previous question, I have attempting using the "codecs" module to give me a little luck. Here's a few examples:
>>> g = codecs.open("C:\Users\Eric\Desktop\beeline.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#39>, line 1)
>>> g = codecs.open("C:\Users\Eric\Desktop\Site.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#40>, line 1)
>>> g = codecs.open("C:\Python31\Notes.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 11-12: malformed \N character escape (<pyshell#41>, line 1)
>>> g = codecs.open("C:\Users\Eric\Desktop\Site.txt", "r", encoding="utf-8")
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (<pyshell#44>, line 1)
My last idea was, I thought it might have been the fact that Windows "translates" a few folders, such as the "users" folder, into Russian (though typing "users" is still the correct path), so I tried it in the Python31 folder. Still, no luck. Any ideas?
The problem is with the string
"C:\Users\Eric\Desktop\beeline.txt"
Here, \U in "C:\Users... starts an eight-character Unicode escape, such as \U00014321. In your code, the escape is followed by the character 's', which is invalid.
You either need to duplicate all backslashes:
"C:\\Users\\Eric\\Desktop\\beeline.txt"
Or prefix the string with r (to produce a raw string):
r"C:\Users\Eric\Desktop\beeline.txt"
Typical error on Windows because the default user directory is C:\user\<your_user>, so when you want to pass this path as a string argument into a Python function, you get a Unicode error, just because the \u is a Unicode escape. If the next 8 characters after the \u are not numeric this produces an error.
To solve it, just double the backslashes: C:\\user\\<\your_user>...
This will ensure that Python treats the single backslashes as single backslashes.
Prefixing with 'r' works very well, but it needs to be in the correct syntax. For example:
passwordFile = open(r'''C:\Users\Bob\SecretPasswordFile.txt''')
No need for \\ here - maintains readability and works well.
With Python 3 I had this problem:
self.path = 'T:\PythonScripts\Projects\Utilities'
produced this error:
self.path = 'T:\PythonScripts\Projects\Utilities'
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
position 25-26: truncated \UXXXXXXXX escape
the fix that worked is:
self.path = r'T:\PythonScripts\Projects\Utilities'
It seems the '\U' was producing an error and the 'r' preceding the string turns off the eight-character Unicode escape (for a raw string) which was failing. (This is a bit of an over-simplification, but it works if you don't care about unicode)
Hope this helps someone
Or you could replace '\' with '/' in the path.
path = pd.read_csv(**'C:\Users\mravi\Desktop\filename'**)
The error is because of the path that is mentioned
Add 'r' before the path
path = pd.read_csv(**r'C:\Users\mravi\Desktop\filename'**)
This would work fine.
I had this same error in python 3.2.
I have script for email sending and:
csv.reader(open('work_dir\uslugi1.csv', newline='', encoding='utf-8'))
when I remove first char in file uslugi1.csv works fine.
Refer to openpyxl document, you can do changes as followings.
from openpyxl import Workbook
from openpyxl.drawing.image import Image
wb = Workbook()
ws = wb.active
ws['A1'] = 'Insert a xxx.PNG'
# Reload an image
img = Image(**r**'x:\xxx\xxx\xxx.png')
# Insert to worksheet and anchor next to cells
ws.add_image(img, 'A2')
wb.save(**r**'x:\xxx\xxx.xlsx')
I had same error, just uninstalled and installed again the numpy package, that worked!
I had this error.
I have a main python script which calls in functions from another, 2nd, python script.
At the end of the first script I had a comment block designated with ''' '''.
I was getting this error because of this commenting code block.
I repeated the error multiple times once I found it to ensure this was the error, & it was.
I am still unsure why.