Not sure if screwed up my python in some way, but simple code below not functioning and not sure how to fix.
Using windows the idea is to open "cmd" in type: name.py books_looking_for and with that get the python to get the word (books_looking_for in this case) and open a browser with that search.
Problem is that the python doesn't "find" the text.
Code below:
import webbrowser, sys
#Get text from cmd
address = ' '.join(sys.argv[1:])
webbrowser.open('https://satakirjastot.finna.fi/Search/Results?lookfor={}&type=AllFields&dfApplied=1&limit=20'.format(address))
Trying with
print(address)
print(type(address))
results are
<class 'str'>
Any ideas how to fix?
Thanks
I'd suggest you to use the Argumentparser from the beginning.
It gives you many advantages, eg. automatic validation options and a autogenerated help page.
from argparse import ArgumentParser
arg_parser = ArgumentParser(description='Run Webbrowser with args')
arg_parser.add_argument('address', help='Look for given address')
args = arg_parser.parse_args()
webbrowser.open('https://satakirjastot.finna.fi/Search/Results?lookfor={}&type=AllFields&dfApplied=1&limit=20'.format(args.address))
Python Manual for ArgumentParser
Why are you slicing sys.argv this way? try simply taking the first index:
#Get text from cmd
address = sys.argv[1]
webbrowser.open('https://satakirjastot.finna.fi/Search/Results?lookfor={}&type=AllFields&dfApplied=1&limit=20'.format(address))
Related
So, I'm writing a basic python script to use youtube-dl to download a highquality thumbnail from a video. With the command line youtube-dl, you can run "youtube-dl --list-thumbnails [LINK]" and it will output a list of different quality links to the thumbnail images. Usually the highest resolution one has 'maxresdefault' in its link. I want to be able to download this image from the command line with wget. This is the code I have so far to achieve it. I'm not familiar with regex, but according to this site: regexr.com, it should have a match in the link with 'maxresdefault'.
import subprocess
import sys
import re
youtubeoutput = subprocess.call(['youtube-dl', '--list-thumbnails', 'https://www.youtube.com/watch?v=t2U2mUtTnzY'], shell=True, stdout=subprocess.PIPE)
print(str(youtubeoutput))
imgurl = re.search("/maxresdefault/g", str(youtubeoutput)).group(0)
print(imgurl)
subprocess.run('wget', str(imgurl))
I put the print statements in there to see what the outputs were. When I run the code, I can see the youtube-dl doesn't recognize a link being in there. youtube-dl: error: You must provide at least one url. Since there's no links in the output, the re.search becomes a NoneType and it gives me an error. I don't know why youtube-dl won't recognize the link. I'm not even sure it recognizes the --list-thumnails. Could anyone help?
You've asked subprocess to use a shell (shell=True), so you would usually pass an entire command to call, like so:
youtubeoutput = subprocess.call("youtube-dl --list-thumbnails https://www.youtube.com/watch?v=t2U2mUtTnzY", shell=True, stdout=subprocess.PIPE)
But really, you may not need a shell. Try something like:
youtubeoutput = subprocess.check_output(['youtube-dl', '--list-thumbnails', 'https://www.youtube.com/watch?v=t2U2mUtTnzY'])
Note that call does not actually return the program's standard output; check_output does.
Reference
I need to check GoldenGate processes' lag. In order to this, I execute Goldengate than I try to run GoldenGate's own commands "info all".
import subprocess as sub
import re
import os
location = str(sub.check_output(['ps -ef | grep mgr'], shell = True)).split()
pattern = re.compile(r'mgr\.prm$')
print(type(location))
for index in location:
if pattern.search(index)!=None:
gg_location = index[:-14] + "ggsci"
exec_ggate = sub.call(str(gg_location))
os.system('info all')
Yet, when I execute the GoldenGate it opens a new GoldenGate's own shell. So, I think because of that, Python cannot be able to do run "info all" command. How can I solve this problem? If there is missing information, please inform me.
Thank you in advance,
For command automation on Golden Gate you have the following information in the Oracle docs: https://docs.oracle.com/goldengate/1212/gg-winux/GWUAD/wu_gettingstarted.htm#GWUAD1096
To input a script
Use the following syntax from the command line of the operating system.
ggsci < input_file
Where:
The angle bracket (<) character pipes the file into the GGSCI program.
input_file is a text file, known as an OBEY file, containing the commands that you want to issue, in the order, they are to be issued.
Taking your script (keep into mind I don't know to code into python) you can simply execute a shell command in python in the following way:
import os
os.system("command")
So try doing this:
import os
os.system("ggsci < input_file")
Changing the input_file as indicated by the docs.
I think you will have an easier time doing it this way.
So I'm using approach in this post
to extract a double quoted string from a string. If the input string comes from terminal argument, it works fine. But if the input string comes from a txt file like the following, it gives nontype error. I tried to get the hash code for two strings(one from file and one from terminal) with identical txt content, and turns out they are different. I'm curious if anyone knows how to solve this?(in Python 3.x)
That said, I have set the default encoding to "utf-8" in my code.
python filename.py < input.txt
If you are using command python, the command recognize it to python 2.x.
If you want python 3.x, just change the command to python3
like this
python3 filename.py < input.txt
Two things, if you want to ingest a txt file into a python script, you need to specify it. Add these two lines
import sys
text = str(sys.argv[1])
this mean text would be your 'input.txt'.
Second, if your script has only a function, it would not know what you want to do with the function, you have to either, tell the script explicity to execute the function through the entry main
import re
import sys
def doit(text):
matches=re.findall(r'\"(.+?)\"',text)
# matches is now ['String 1', 'String 2', 'String3']
return ",".join(matches)
if __name__ == '__main__':
text_file = str(sys.argv[1])
text = open(text_file).read()
print(doit(text))
Alternately, you can just execute line by line without wrapping the re in a function, since it is only one line.
I just figure it out, the bug doesn't come from my code. I had the "smart quotes" enabled on my Mac so whenever it reads a quote, it's identified as a special character. Disable this under keyboard setting would do the trick.
LOL what a "bug".
My apologies if I haven't used correct terminology.
I am writing a small python script to copy files from external harddrive to Mac.
I get the external hard drive list by using -
import os
os.listdir('/Volumes')
I get the following list -
BOOTCAMP
FREEAGENT
Mobilebackup
PCQ
PCQ....is DVD.MobileBackup, not sure what it is. I cant see it in left pane of finder. I want to know the type of volume or device, so that I can just use the external hard-drive and ignore everything else.
Is there a way to do it?
Have a look at the output of diskutil list, perhaps you can parse it to get what you want.
For parsing diskutil you can specify the -plist option and then use plistlib to convert to a Python dictionary:
import plistlib
import pprint
import subprocess
output = subprocess.check_output(('/usr/sbin/diskutil', 'list', '-plist'))
diskutil = plistlib.readPlistFromString(output)
pprint.pprint(diskutil)
I have a text file with a list of about 50 hostnames and I am looking to script a way to run through them to get each associated IP address in the Command Prompt.
I thought pasting the hostname list in to the following code might be the easiest way but socket.gethostbyname will take no more than 1 argument at a time.
import socket
socket.gethostbyname("***hostnames***")
Is there a way to work around this argument issue, or is there a way to have the hostnames read from the textfile?
The easiest work around is to pass a filename and iterate through it:
#!/usr/bin/python
import sys
import socket
file_nm = sys.argv[1]
with open(file_nm, 'r') as f:
for host in f:
print socket.gethostbyname(host.strip())