I'm trying to create a python script to auto update a program for me. When I run program.exe --help, it gives a long output and inside the output is a string with value of "Version: X.X.X" How can I make a script that runs the command and isolates the version number from the executable's output?
I should have mentioned that I tried the following:
import re
import subprocess
regex = r'Version: ([\d\.]+)'
match = re.search(regex, subprocess.run(["program.exe", "--help"]))
print((match.group(0)))
and got the error:
Traceback (most recent call last):
File "run.py", line 6, in <module>
match = re.search(regex, subprocess.run(["program.exe", "--help"]))
File "C:\Python37\lib\re.py", line 183, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object
Something like this should work:
re.search(r'Version: ([\d\.]+)', subprocess.check_output(['program.exe', '--help']).decode()).group(1)
Related
While the following code snippet of a python program generates a nice xml file:
from proc import create_SRXML
create_SRXML.create_xml()
This following does not generate the xml file (the line "import pumoni.visu.renders as visua" is spoiling the job)
from proc import create_SRXML
import pumoni.visu.renders as visua
create_SRXML.create_xml()
and getting error log as follows:
Traceback (most recent call last):
File "processor/analyze_all.py", line 26, in <module>
create_SRXML.create_xml()
File "/datas/repo/work/pul/process/create_SRXML.py", line 13, in
create_xml
b1 = pulvii.SubElement(m1, "element",len="4",
name="FileMetaInformationGroupLength", tag="0002,0000", vm="1",
vr="UL")
TypeError: SubElement() got multiple values for argument 'tag'
May I know what the problem here is?
I have a Python script obtained from a project which I am trying to debug however I am unable to resolve one error. Per the author's description of the project, everything works fine.
The script takes a parameter called "ascii" which is of type str as shown below:
parser.add_argument('--ascii', type=str,
help='ASCII Data type: ASCII characters')
Per my understanding, in the following code, it processes the input string one character at a time and each character is sent to a function, iter_bin() which will take the ASCII value of the character and convert it to binary, appending the output to a list.
ASCIIDATA = args.ascii
dataArray = []
for line in ASCIIDATA:
for entry in line:
# Make sure everything is a number, convert if not
dataArray.append(''.join(s for s in iter_bin(entry)))
def iter_bin(s):
sb = s.encode('ascii')
return (format(b, '07b') for b in sb)
When I run this code, I get the following error:
Traceback (most recent call last):
File "check.py", line 107, in <module>
main()
File "check.py", line 70, in main
dataArray.append(''.join(s for s in iter_bin(entry)))
File "check.py", line 70, in <genexpr>
dataArray.append(''.join(s for s in iter_bin(entry)))
File "check.py", line 82, in <genexpr>
return (format(b, '07b') for b in sb)
ValueError: Unknown format code 'b' for object of type 'str'
How can I resolve this error?
Thanks.
For this code I am converting a working python webcrawler from 2.7 to 3.4. I've made some modifications but I still get errors when running it:
Traceback (most recent call last):
File "Z:\testCrawler.py", line 11, in <module>
for i in re.findall('''href=["'](.[^"']+)["']''', urllib.request.urlopen(myurl).read(), re.I):
File "C:\Python34\lib\re.py", line 206, in findall
return _compile(pattern, flags).findall(string)
TypeError: can't use a string pattern on a bytes-like object
This is the code itself, please tell me if you see what the syntax errors are.
#! C:\python34
import re
import urllib.request
textfile = open('depth_1.txt','wt')
print ("Enter the URL you wish to crawl..")
print ('Usage - "http://phocks.org/stumble/creepy/" <-- With the double quotes')
myurl = input("#> ")
for i in re.findall('''href=["'](.[^"']+)["']''', urllib.request.urlopen(myurl).read(), re.I):
print (i)
for ee in re.findall('''href=["'](.[^"']+)["']''', urllib.request.urlopen(i).read(), re.I):
print (ee)
textfile.write(ee+'\n')
textfile.close()
Change
urllib.request.urlopen(myurl).read()
to for example
urllib.request.urlopen(myurl).read().decode('utf-8')
What happens here is .read() returning bytes instead of str like it was in python 2.7, so it has to be decoded using some encoding.
So I was trying to answer a question on SO when I ran into this issue. Basically a user had the following string:
Adobe.Flash.Player.14.00.125.ie
and wanted to replace it with
Adobe Flash Player 14.00.125 ie
so I used the following re.sub call to solve this issue:
re.sub("([a-zA-Z])\.([a-zA-Z0-9])",r"\1 \2",str)
I then realized that doesn't remove the dot between 125 and ie so I figured I'd try to match another pattern namely:
re.sub("([a-zA-Z])\.([a-zA-Z0-9])|([0-9])\.([a-zA-Z])",r"\1\3 \2\4",str)
When I try to run this, I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/re.py", line 151, in sub
return _compile(pattern, 0).sub(repl, string, count)
File "/usr/lib64/python2.6/re.py", line 278, in filter
return sre_parse.expand_template(template, match)
File "/usr/lib64/python2.6/sre_parse.py", line 793, in expand_template
raise error, "unmatched group"
sre_constants.error: unmatched group
Now, I understand that it's complaining because I'm trying to replace the match with an unmatched group but is there a way around this without having to call re.sub twice?
Without any capturing groups,
>>> import re
>>> s = "Adobe.Flash.Player.14.00.125.ie"
>>> m = re.sub(r'\.(?=[A-Za-z])|(?<!\d)\.', r' ', s)
>>> m
'Adobe Flash Player 14.00.125 ie'
I am trying to input a path using optparser in python. Unfortunately this piece of code keeps showing an error.
import optparse,os
parser = optparse.OptionParser()
parser.add_option("-p","--path", help = "Prints path",dest = "Input_Path", metavar = "PATH")
(opts,args) =parser.parse_args()
print os.path.isdir(opts.Input_Path)
Error :-
Traceback (most recent call last):
File "/Users/armed/Documents/Python_Test.py", line 8, in
print os.path.isdir(opts.Input_Path)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py", line 41, in isdir
st = os.stat(s)
TypeError: coercing to Unicode: need string or buffer, NoneType found
Any help is much appreciated !
That error is because opts.Input_Path is None, instead of being your path string/unicode.
Are you sure you are calling the script correctly? You should probably put in some error checking code in any case to make sure that if a user doesnt put -p, the program won't just crash.
Or, change it to a positional argument to make it 'required' by optparse:
http://docs.python.org/library/optparse.html#what-are-positional-arguments-for
Edit: Also optparse is deprecated, for a new project you probably want to use argparse.
I copied your script and ran it. Looks like you call your script in a wrong way:
$ python test.py /tmp
Traceback (most recent call last):
File "test.py", line 8, in <module>
print os.path.isdir(opts.Input_Path)
File "/usr/lib/python2.6/genericpath.py", line 41, in isdir
st = os.stat(s)
TypeError: coercing to Unicode: need string or buffer, NoneType found
but
$ python test.py --path /tmp
True