Have Python script read a text file instead of single input - python

I am working with this python script: https://github.com/sarchar/addressgen/blob/master/genaddress.py
Currently it works via command line like so:
python3 genaddress.py -p passphrase
How would I alter the script to accept a text file of passphrases?

I know this might not directly answer the question (How would I alter the script), but it should achieve a similar result on bash with the following command, assuming each passphrase has its own unique output:
cat passphrases.txt | xargs -I phrase python3 genaddress.py -p phrase
This iterates through each line in passphrases.txt and then subsequently passes it to your script, one line at a time.

Related

Extract file name after '>' from command line in python

How can I extract the filename after sign '>' from a command line in python. For example:
python func.py -f file1.txt -c > output.txt
I want to get the output file name 'output.txt' from the above command line.
Thanks
You can't.
When you write something like command > file in shell, the shell:
creates the file if it doesn't exist,
opens it, and
assigns the descriptor to the stdout of command.
The called process (and it doesn't matter if that's Python or something else) doesn't know what happens to the data after it's written because it only sees the descriptor provided by the shell. It's a bit like having one end of a really long pipe: you know you can put stuff in, but you can't see what happens to it on the other end.
If you want to retrieve the file name in that call, you need to either:
pass it to your Python program as an argument and handle redirection from Python, e.g. python func.py -f file1.txt -c output.txt, or
output it from the shell, e.g. echo 'output.txt'; python func.py -f file1.txt -c > output.txt

Python Script input via Bash Shell

I have a python script which i am calling from bash script and this bash script get call from cron
#!/bin/bash
set -o errexit
set -o xtrace
echo "Verify/Update Firmware"
/usr/bin/python -u /usr/bin/Update.py
Now when this python run it ask for some input(from keyboard), but i am not able to capture it. How my python can get input in this scenario?
Python script look like below
ip = raw_input('Enter IP for Switch')
tn = telnetlib.Telnet ( ip, 23, 600 )
For giving command line arguments to a bash script you can use $1, $2, $3 etc. The tutorial here talks about this: http://linuxcommand.org/lc3_wss0120.php
For the python part you can use something like argparse to do this pretty nicely. This also had loads of tutorials out there.
For a single line of input use this:
echo "input" | command arg1 arg2
For multiple lines write the expected input to a file, then redirect the input:
command arg1 arg2 < inputfile
It is not guaranteed to work depending on many details.
Please consider the risk of blindly giving input without reading what the program wants.
For a more sophisticated solution check the expect utility.

Trying to get python to run a program in terminal and output to a file

Here's what I've got so far...
import os
os.system("lxterminal --command='sudo netdiscover -p -PL >>
/home/pi/data.txt'")
The issue that I'm having is when I execute this code it doesn't write out to a file like it would if I just put it into a terminal window. Instead, it opens two terminal windows, one titled sh that shows that the code executed without a problem, followed by a second one titled data.txt that does nothing. Removing the single quotes from before sudo and after txt causes it to write out to a file, but only to tell me how to use commands with the LXTerminal. How can I make it write out and append to a file (called anything, doesn't have to be data.txt)?
import subprocess
subprocess.Popen(args=["xterm", "-e", "/bin/echo hi >> /home/user/data.txt"], shell=True)

command line python script run on a file in different directory

I have a script.py in /Users/admin/Desktop and I want to run this script on a file that is in /Users/admin//Desktop/folder/file.txt, without changing the present dir.
Question: what is the most efficient way to do that on command-line ? I am using the following commands and results are not as expected.
$ python script.py --script; /Users/admin/Desktop/file.txt
raise StopIteration('because of missing file (file.txt)')
StopIteration: because of missing file (file.txt)
Remove the semicolon because that will prematurely terminate the command.
Pass the correct path to the file to your program. You say it is /Users/admin/Desktop/folder/file.txt, however, your command is using /Users/admin/Desktop/file.txt (it's missing folder)
So the command should (probably) be:
$ python script.py --script /Users/admin/Desktop/folder/file.txt
If that doesn't work you will need to edit your question to show your code.

How can I send line number from Python traceback into vim?

I can parse out the paths to the files of a Python traceback, and I can then send those on to Vim using -p on the command line, so that they are opened one file per tab. So I end up with a command like, for example
vim -p main.py module.py another.py
That opens each file in a new tab, but I would like them opened in a new tab, at the correct line number. So I have tried variations like
vim -p main.py +10 module.py +20 another.py +30
But I cannot seem to get Vim to respect the line numbers I send in on command line - it always just takes the last line number and applies it to first tab. So the example left me in main.py at line 30. Trying variations like
vim -p main.py+10 module.py+20 another.py+30
vim -p main.py\ +10 "module.py +20" another.py#30
all just ended up with bad filenames.
Answers at the level of Python, or Bash command line, or within Vim script, or Vim-Python would all be welcome. Or, indeed, entirely different approaches
(The tracebacks could come from anywhere, and are not necessarily controllable by me. The one that started me off today was just a set of lines in a log from a server.)
Try vim plugin: file_line:
vim -p new main.py:10 module.py:20 another.py:30
Known Issue: the first filename should not have a lineno. (I'm trying to figure out WHY...)

Categories