I am trying to run a C executable say c4.5 and c4.5rules which will take a single command line argument inside a python code using os.system, I am using the C executable written by some other person, and I don't have the original .c file with me. I need to write like below to make sure that executable file is doing okay.
import sys
import os
dataset = sys.argv[1]
os.system(f"/home/Dev/c4.5 -u -f {dataset}")
os.system(f"/home/Dev/c4.5rules -u -f {dataset}")
os.system(f"/home/Dev/c4.5rules -u -f {dataset} > Temp")
f = open('Temp')
...
And if I remove that -u and -f, then it is not working.
Why is it happening and what is the use of -u and -f?
Related
I am tying to concatenate all mpeg files together in one new file in windows 7, I adjusted the environment variables and running the code from python shell but it gives invalid syntax. Any help as I am new to Python and ffmpeg library?
My code:
ffmpeg -f concat -i <(for f in glob.glob("*.mpeg"); do echo "file '$PWD/$f'"; done) -c copy output.mpeg
Thanks
Your example code is mix or Python code and Bash code so it can't run in Python Shell nor in Bash Shell :)
On Linux it works in Bash as two commands:
(Windows probably doesn't have printf command)
printf "file '%s'\n" *.wav > input.txt
ffmpeg -f concat -i input.txt -c copy output.mpeg
Python version which doesn't need Bash:
#!/usr/bin/env python3
import os
import sys
import glob
import subprocess
# get Current Working Directory (CWD)
pwd = os.getcwd()
# get list of files
if len(sys.argv) > 1:
#filenames = sys.argv[1:] # Linux
filenames = glob.glob(sys.argv[1]) # Windows
else:
filenames = glob.glob("*.mpg")
#print(filenames)
# generate "input.txt" file
with open("input.txt", "w") as f:
for name in filenames:
f.write("file '{}/{}'\n".format(pwd, name))
#f.write("file '{}'\n".format(name))
# run ffmpeg
subprocess.run('ffmpeg -f concat -i input.txt -c copy output.mpeg', shell=True)
And you can run it with or without argument ie. "*.wav"
python script.py *.wav
(tested only on Linux)
printf (and other Bash commands) for Windows: GnuWin32
More on GnuWin32
I am executing python script with multiple command line parameter, but using shell script.
command i execute for shell script execution is:
./scripts/run_qa.sh data/questions/questions.txt data/lexicons/paralex data/weights/paralex.txt data/database > output.txt
run_qa.sh files looks like below (please explain how it works):
#!/bin/bash
set -u
set -e
if [ $# != 4 ]; then
echo "Usage: run.sh questions lexicon weights db"
exit 1
fi
questions=$1
lexicon=$2
weights=$3
db=$4
PYTHONPATH=$PWD/python python -m lex.gearman_worker $lexicon $weights $db < $questions
I tried to execute python command as below in Command line :
python -m python/lex/gearman_worker.py data/lexicons/paralex data/weights/paralex.txt data/database > output.txt
which gives error :
/usr/bin/python: Import by filename is not supported.
Update1 :
gearman_worker.py file import other files like ths:
import lex.parse
import lex.semantics
from collections import namedtuple
from collections import defaultdict
import line gives error like this:
ImportError: No module named lex.lexicon
Update2 (executed on linux terminal):
export PYTHONPATH=$/mnt/paralex-evaluation-gearman/python
PYTHONPATH = ./python python -m python/lex/gearman_worker data/lexicons/paralex data/weights/paralex.txt data/database > output.txt
gives:
PYTHONPATH: command not found
Then
python -m python/lex/gearman_worker data/lexicons/paralex data/weights/paralex.txt data/database > output.txt
gives:
File "/mnt/paralex-evaluation-gearman/python/lex/gearman_worker.py", line 3, in <module>
import lex.lexicon
ImportError: No module named lex.lexicon
You just need to execute the following command:
PYTHONPATH=./python python -m lex.gearman_worker ARGUMENT_2 ARGUMENT_3 ARGUMENT_4 < ARGUMENT_1
If that doesn't work then you may have to export the PYTHONPATH setting:
export PYTHONPATH=${PWD}/python
python -m lex.gearman_worker ARGUMENT_2 ARGUMENT_3 ARGUMENT_4 < ARGUMENT_1
The original arguments that you would pass to the script are listed as ARGUMENT_N.
The script just:
sets some sensible defaults (see the documentation for set)
tests the the right number of arguments have been supplied
invokes the command above
Your attempt to invoke it:
misses the PYTHONPATH setting which is present in the script
passes the gearman_worker module as a file rather than a python module import
I want to pass the name of a file to a python script while I'm running it from the command line. I'm trying this clear command:
cat enwiki-latest-pages-articles.xml | python WikiExtractor.py -b 500K -o extracted
however, it gives an error:
'cat' is not recognized as an internal or external command, operable program or batch file.
Thanks in advance.
It seems like you're running the command in Windows. In windows, there's no cat installed unless you installed.
You can use type command instead:
type enwiki-latest-pages-articles.xml | python WikiExtractor.py -b 500K -o extracted
The correct way would be python WikiExtractor.py -b5 -o extracted -f enwiki-latest-pages-articles.xml.
And use sys.argv array of input arguments of python command from sys command.
This may help:
http://www.tutorialspoint.com/python/python_command_line_arguments.htm
I am newbie to python, and for GUIs, I use wxpython.
My Issue is this:
I have to create a debian file for two types of products(say product 1 and product 2).That can be done by running README.package.creation file. For "product1" in ".bashrc" we have to change
Product = product1
After that we have to do "make clean" in new terminal(otherwise changes in .bashrc will not take effect i.e "product" may not be equal to "product 1" if we dont follow the procedure), then we have to run ./Readme.package.creation.process. In Readme.package.creation then it takes automatically product type as "product 1"
If I does this manually it will work fine but if i do this through GUI it Readme.package.creation file will not take product type. From python null value will be sent.
Please help to solve my issue.
My code is:
subprocess.call("sed -i '/export PRODUCT/d' .bashrc", shell=True)
subprocess.call("sed -i '/export BOARD=TYpe/ a\ export PRODUCT=product1' .bashrc", shell=True)
os.chdir("/home/x/y/z")
subprocess.call("make clean", shell=True)
os.chdir("/home/x/main/src/package")
subprocess.call("sed -i 's/re.build -f -gui -p all/re.build -gui -p all -svn no/' README.package.creation", shell=True)
subprocess.call("gksu debian", shell=True)
subprocess.Popen("xfce4-terminal -e 'bash -c \"./README.package.creation -u %s\";sleep 10'" % (str(u_name)),shell=True)
How to do after that I have to follow same procedure for Product 2 also
EDIT:
How about os.environ in python?
I have tried to change with os.putenv and then os.environ seems like it doesnot work fine.
Try:
import OS
os.environ['product']='product1'
subprocess.call("make clean", shell=True)
and so on
Your problem is very simple, and so is the solution:.
In the subprocess.Popen(...), change the call from:
subprocess.Popen("xfce4-terminal -e 'bash -c \"./README.package.creation -u %s\";sleep 10'" % (str(u_name)),shell=True)
to:
subprocess.Popen("xfce4-terminal -e 'bash -c \"source ~/.bashrc; ./README.package.creation -u %s\";sleep 10'" % (str(u_name)),shell=True)
Essentially, you're asking bash to source the .bashrc file before calling the package creation command.
Another illustration:
sgulati#precise:~$ cat /tmp/1.sh
export A=100
sgulati#precise:~$ python -c "import subprocess
print subprocess.Popen(['bash', '-c', 'source /tmp/1.sh; echo \$A'], stdout=subprocess.PIPE).stdout.read()"
100
In this example, I declare the variable A=100 in /tmp/1.sh, source it and then execute echo $A. Because of source /tmp/1.sh, the value of A is known when echo $A is executed.
Please note that the syntax I used in my example is the syntax from python 2.7.3, but the concept is pretty much identical, no matter how you go about it.
I want to run a command like this
grep -w 1 pattern <(tail -f mylogfile.log)
basically from a python script i want to monitor a log file for a specific string and continue with the python script as soon as i found that.
I am using os.system(), but that is hanging. The same command in bash works good.
I have a very old version of python (v2.3) and so don't have sub-process module.
do we have a way to acheive this
In Python 2.3, you need to use subprocess from SVN
import subprocess
import shlex
subprocess.call(shlex.split("/bin/bash -c 'grep -w 1 pattern <(tail -f mylogfile.log)'"))
To be explicit, you need to install it from the SVN link above.
You need to call this with /bin/bash -c due to the shell redirection you're using
EDIT
If you want to solve this with os.system(), just wrap the command in /bin/bash -c since you're using shell redirection...
os.system("/bin/bash -c 'grep -w 1 pattern <(tail -f mylogfile.log)'")
First of all, the command i think you should be using is grep -w -m 1 'pattern' <(tail -f in)
For executing commands in python, use the Popen constructor from the subprocess module. Read more at
http://docs.python.org/library/subprocess.html
If I understand correctly, you want to send the output to python like this -
tail -f mylogfile.log | grep -w 1 pattern | python yourscript.py
i.e., read all updates to the log file, and send matching lines to your script.
To read from standard input, you can use the file-like object: sys.stdin.
So your script would look like
import sys
for line in sys.stdin.readlines():
#process each line here.