This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 4 years ago.
I found myself doing a repetitive task and wondered how I can automate it.
I can write a function script with python(3), to iterate through each item in a folder, but I'm not sure how I would run that through command prompt.
I have sort of looked into how it would be possible, but I think a direct response to my exact question would be more helpful and easier to grasp.
My question comes more from a desire to learn than it does from laziness!
You could use os.system, but subprocess.run is probably better. You should also use glob:
import glob
import subprocess
files = glob.glob('*.wav')
for file in files:
subprocess.run(['xWMAEncode', file, file.replace('.wav', '.xwm')])
Related
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 5 years ago.
There are mainly two questions that I would like to ask, thanks in advance.
(1) How can I open an external program in Linux?
I know in Windows there is a command os.startfile() to open another program, the equivalent for Ubuntu is open(), but there's no response after I run the code, and the alternative one is subprocess.call(). This works well in Windows, but in Ubuntu it fails, could someone provide a standard templete I can use for? (Similarly like to double click the icon of a program)
(2) How can I realize functions like the code is able to open the terminal and write down several commands in terminal automatically using python?
os.system can do this work. for example, you want to run 'ls' under a shell. want_run='ls';os.system('bash -c '+ want_run);
(1)
You can use proc = subprocess.Popen(command, stdout=subprocess.PIPE) and afterwards run proc.stdout.read() to get the output of the command run.
See the subprocess documentation https://docs.python.org/2/library/subprocess.html
(2)
Please provide more info on this question(examples) for what you want to do.
This question already has answers here:
Get list of open files (descriptors) in OS X
(9 answers)
Closed 5 years ago.
In terminal how do I output which files a process is calling upon? For instance, if I was using Adobe Premiere and I wanted show which project file Premiere had open via terminal.
Not sure what you mean by calling upon. If you are looking for all the files currently opened by your process, use lsof:
lsof -p PID
where PID is the ID of the process you are looking at.
This question already has answers here:
Reusing output from last command in Bash
(14 answers)
Using output of previous commands in bash
(5 answers)
Reference stdout (i.e. output of previous command) quickly in bash?
(2 answers)
Closed 5 years ago.
Is there any way to print the last printed lines on a linux terminal? I'm trying to run a command that does that on my node app so that i can parse that content. Is all the shell content logged in a file, or something like that?
I want to echo the console own content. Get the last outputs of the own terminal.
I dont want command outputs I want to get console.logs() and console.errs()
You need to save it by urself. There are history option for Command you used but not for output unless it has been saved by other program in case of error.
You can use top command. It tells a lot much information regarding the system. You can use ps command and get the information about running processes and then use cat command to store that output in some other file or you can even use pipeline symbol to redirect its output to some file.
This question already has answers here:
Working with huge files in VIM
(10 answers)
How to edit multi-gigabyte text files? Vim doesn't work =( [closed]
(15 answers)
Closed 6 years ago.
I have a db dump file that is over 5 gigs in size and I'm looking to do a quick edit to the create database and use database command. This dump is provided to me.
I've been using vim to do this from the command line, but it takes a while to load. I'm able to use less to read very quickly. Is there a way to edit the file without having to wait several minutes for the full file to load in vim? This can be a param passed to vim, or different common way to edit files from command line.
I'm looking for a general solution that I can apply to other large files too, so would like a linux command that would allow me to edit the top of the file quickly.
You can use cat:
cat file_with_create_cmd db_dump > new_dump
If you want to use that in a subsequent command instead of writing it to a file, you may use process substitution:
process_dump <(cat file_with_create_cmd db_dump)
This question already has answers here:
Renaming multiples files with a bash loop
(3 answers)
Closed 8 years ago.
I have a directory, let's say its name is direct in which has multiple files whose names are .xyz, .abc, .klm and .etk4 etc. Is there a way to make all those hidden files visible at the same time instead of one by one? I know this question has been asked before here but I did not get the answer. I hope somebody can explain it to me in a simple way since I am not much familiar with linux.
for file in .[^.]*
do
mv "${file}" "${file#.}"
done
${var#prefix} expands to the value of $var with the initial prefix removed.