TypeError: startswith first arg must be bytes or a tuple of bytes, not str
See original GitHub issuepython 3, winrm version 0.2
code
command = 'mkdir C:/a/b/c'
s = winrm.Session(hostname, auth=(username, password))
r = s.run_ps(command)
If command is ‘ls C:/a/b’ ,then success.
Traceback
Traceback (most recent call last):
File "/usr/lib64/python3.4/site-packages/tornado/web.py", line 1443, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "/g/ops_card-master/handler/business/script.py", line 40, in post
result = self.exec_script(pk)
File "/g/ops_card-master/handler/decorator.py", line 4, in wrapper
result = func(*args, **kwargs)
File "/g/ops_card-master/handler/business/script.py", line 136, in exec_script
result = self.pool.starmap(self.exe, li)
File "/usr/lib64/python3.4/multiprocessing/pool.py", line 268, in starmap
return self._map_async(func, iterable, starmapstar, chunksize).get()
File "/usr/lib64/python3.4/multiprocessing/pool.py", line 599, in get
raise self._value
File "/usr/lib64/python3.4/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/usr/lib64/python3.4/multiprocessing/pool.py", line 47, in starmapstar
return list(itertools.starmap(args[0], args[1]))
File "/g/ops_card-master/handler/decorator.py", line 18, in wrapper
result = func(*args, **kwargs)
File "/g/ops_card-master/handler/business/script.py", line 172, in exe
r = s.run_ps(command)
File "/usr/lib/python3.4/site-packages/pywinrm-0.2.0-py3.4.egg/winrm/__init__.py", line 54, in run_ps
rs.std_err = self._clean_error_msg(rs.std_err)
File "/usr/lib/python3.4/site-packages/pywinrm-0.2.0-py3.4.egg/winrm/__init__.py", line 62, in _clean_error_msg
if msg.startswith("#< CLIXML\r\n"):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
Issue Analytics
- State:
- Created 7 years ago
- Reactions:9
- Comments:8
Top Results From Across the Web
startswith TypeError in function - python - Stack Overflow
It's because you're opening the file in bytes mode, and so you're calling bytes.startswith() and not str.startswith() . You need to do line.startswith(b'>') ......
Read more >How to Solve startswith first arg must be str or a tuple of str, not ...
If we read the TypeError: startswith first arg must be str or a tuple of str, not (bool/list/int) error carefully, we will see...
Read more >Diff. between Py 2.7 and 3 - Python Forum
TypeError : startswith first arg must be bytes or a tuple of bytes, not str ... str.startswith(str, beg = 0,end = len(string)); so,...
Read more >Bug #22816: TypeError: startswith first arg must be bytes or a ...
Thus, the sentence at osd.py:375 ( if line. startswith('Disk /'): ) fails because we are trying a string against startswith .
Read more >24884 (Migrations created in Python2.7 fail in Python3.4)
... line 98, in quote_name if name.startswith('"') and name.endswith('"'): TypeError: startswith first arg must be bytes or a tuple of bytes, not str....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I hit the same issue, but my tweak was a little different:
I am on python3.4 and version 0.2.0
Output:
Changing code as suggested above gave me this message:
So… warning looked suspicious…
I removed the b
I changed the message before it went into
def _clean_error_msg(self, msg):
I changed rs.std_err in
def run_ps(self, script):
from: rs.std_err = self._clean_error_msg(rs.std_err) to: rs.std_err = self._clean_error_msg(rs.std_err.decode(‘utf-8’))
I now have a clean output.
Hope this helps.
If anyone is looking for a quick monkey patch based on @bikerider262’s solution, this worked for me