Runnig install.sh file with python [duplicate] - python

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 1 year ago.
How can i run install.sh file using python code.Purpose is to implement software update functionality.The sh file is generated using makeself and it is a self-extractable archive.I get this output when i use os.system or subprocess.run
Failed to parse arguments: Unknown option -title
NB: The Script file don't require any arguments

subprocess.call (Python <=3.5) and subprocess.run (Python >3.5) would be a safer alternative to os.system.

Solution 1 : Use SubProcess
import subprocess
subprocess.call(['/path/to/your/script.sh', 'argument1', 'argument2', ..., 'argumentn'])
Solution 2 : Use Os
import os
os.system('/path/to/your/script.sh argument1 argument2 ... argumentn'])
Both will work fine, if you have the choice it's better to use subprocess since it will handle for you the formatting of your command with thing such as space in the command line or special characters.

Related

Interaction with linux console in python [duplicate]

This question already has answers here:
Running shell command and capturing the output
(21 answers)
Closed 2 years ago.
How I can interaction with linux console in python. Open (or not) window with console, and do any command or just typing something and press enter
For example:
import os
os.system('ls')
I like to use both subprocess and os.
These python modules dont necessarily 'open a console and run a command' rather they interface with your shell.
import os
import subprocess
eg
fileCount = int(subprocess.check_output('ls dir/ | wc -l', shell=True).decode().strip())
or
pathToOldest = './someDir/' + oldestFileInDir
os.remove(pathToOldest)
If youre looking to do run a particular command I would go to a search engine and type 'python run shell commmands' or 'run {enter shell command} in pythonX}

Pass file paths from Python to shell script [duplicate]

This question already has answers here:
Actual meaning of 'shell=True' in subprocess
(7 answers)
Closed 8 months ago.
I would like to run a shell script from Python 3 in Linux passing two arguments that contain file paths to two different files. The shell script then calls a programme written in Python 2.
In Python 3, I call the shell script like this:
import os
import sys
os.chmod('/path/to/sh', 0o777)
subprocess.call(['/path/to/sh', '/path/to/file1', '/path/to/file2'], shell=True)
My shell script looks like this:
#!/bin/sh
set -x
path1=$1
path2=$2
python2 path/to/programme "$path1" "$path2"
Now, the file paths are empty, and the shell script returns something like python2 path/to/programme '' ''. Does someone know how I could correctly pass the file paths so that the programme written in Python 2 can read them?
Or is there even an easier solution such as using subprocess to directly call the programme written in Python 2?
There is no need for the shell script. You can use subprocess to run python2 directly.
a.py
#!/usr/bin/env python3
import subprocess
subprocess.call(['python2', './b.py', 'foo', 'bar'])
b.py
#!/usr/bin/env python2
import sys
print sys.argv
Running ./a.py outputs ['./b.py', 'foo', 'bar'].
You could also try using past.translation instead:
The past module provides an experimental translation package to help with importing and using old Python 2 modules in a Python 3 environment.
shell=True is only needed if you do something like
subprocess.run("/path/to/sh /path/to/file1 /path/to/file2", shell=True)
where the shell will split the single string into arguments that will identify as the program name and its arguments. But you already have the program name and its arguments identified, so
subprocess.run(['/path/to/sh', '/path/to/file1', '/path/to/file2'])
is all you need.
By using a list and shell=True, you are essentially asking Python to execute
sh -c /path/to/sh /path/to/file1 /path/to/file2
which uses /path/to/file1 to set the value of $0, not $1, in the command to execute.

Can you run a bash command calling a python file with variables? [duplicate]

This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 2 years ago.
I'm looking to run a bash command that runs a python file ie: "python /Users/declanmcgranahan/airflow/dags/tasks/aircall_calls_to_csv.py". But I have a variable in that python file that I want to define in the bash command. Is this possible?
Do you mean something like python test.py var1 var2?
Take a look at this link: Python - Command Line Arguments
This even provides options function like python test.py -a var1 -b var2
You can pass arguments to the bash command and then read in those arguments into required variables in the __main__ handler function. Look up argparse library on how to use it to handle provided arguments. The code in your python file should have a block:
if __name__ == "__main__":
# use argparse to assign variables
you could pass a parameter to the python script on the command line, or you could set an environment variable in the script and have the script read it using getenv.
Command-line arguments: How to read/process command line arguments?
getenv: https://docs.python.org/3/library/os.html#os.getenv

How to give python command line arguments from command line mode

I am attempting to start the python interpreter by typing python into my terminal. I also want to pass some command line arguments. If I try python arg1 it things I am trying to open and run a script called arg1. So my question is, how can I pass command line arguments to python in the command line interpreter mode?
You already are.
Your arg1 is a command line argument - to the python interpreter.
Maybe you're thinking of a command line option? (which really is just a specific type of command line argument)
Using the -h argument (python -h) will print all such options.
python -Q new, for example, will launch the interpreter with the new float division by default.
On the other hand, if you want to pass command line arguments to your script, you need to specify them after the script name: python my_script.py arg1
If you are on a UNIX based system, you can also use a shebang on your script, which will allow you to simply execute your script by name, skipping python:
#!/usr/bin/env python
#your script here
then run ./my_script.py arg1
An easy way to do it is by using the subprocess module and input variables.
An example would be:
from subprocess import call
x = raw_input('Which command line arguments? ')
call(x, shell=True)
Try it with an ls command (dir for Windows users), it should work.
You can access the command line arguments using sys.argv
import sys
print sys.argv
% python scrpt.py a b c d
outputs: ['scrpt.py', 'a', 'b', 'c', 'd']
Use - to mark the end of python's interpreter options:
python --interpreter-option-1 --interpreter-option-2 - --my-custom-option option-value my-custom-argument

Passing Command line argument to Python program using IDLE? [duplicate]

This question already has answers here:
When running a python script in IDLE, is there a way to pass in command line arguments (args)?
(12 answers)
Closed 10 years ago.
I have downloaded a python file xxxxxx.py that is supposed to run on the command line by
typing: python xxxxxx.py filename1 filename2
and that should take these two files as arguments.
I was wondering if there is a way I can use IDLE to pass in these arguments. Is there a way other than setting sys.argv ?
Thanks
It depends on the content of your Python file. If it is well-written, like:
#! /usr/bin/env python
def process(files):
for file in files:
# ...
if __name__ == '__main__'
# some error checking on sys.argv
process(sys.argv[1:])
sys.exit(0)
Then you could simply import the python file and run it like:
import name_of_file
# ...
name_of_file.process([file1, file2, file3])
# ...
So, it really depends on how it is written. If it isn't written well but you can edit it, I would refactor it so that it can be used as a library; otherwise, I would use the subprocess module to invoke the program.
You can do this from the command line with:
idle.py -r scriptname.py put arguments here
You can try a different IDE like ActivePython
Or you can patch IDLE:
http://bugs.python.org/issue5680

Categories

Resources