i'm new to Linux.
I am learning Linux commands.
I am looking for a way to write command lines after each other from text file for instance.
Instead of typing five or more lines by self i can just call the text file to write commands after each other.
Is this even possible?
Is this a known thing?
If there is anything like please guide me to it.
Thanks in advance.
You can create it by using the subprocess module.
Example :
import subprocess
subprocess.call(["ls"])
subprocess.call(["touch","helloworld.txt"])
# You can enter commands by using subprocess.call()
The you use chmod +x filename.py to make the file executable and the ./filename.py to run the script
Or as suggested by #bipll in the comments, creating an .sh file. Here is the tutorial if you need.
Related
Why can't we open the .py file with its path by python?
noob asking. Apologies if I am stupid.
You don't.
If you have the file /home/user/scripts/tests/main.py, with the following content:
print("Hello World")
Running python3 /home/user/scripts/tests/main.py will run the python script as expected.
There may be an issue with your PATH or with how you're entering the path to the file
You probably use similar construction:
myscript.py
It means, that terminal consider your input as a command, and tries to find in in $PATH.
Firstly you need to use absolute or relative addressation:
./myscript.py
# or
/path/to/file/myscript.py
Secondly, you need to make sure, your script has an executable bit:
chmod +x myscript.py
Thirdly, make sure you use shebang in the first line of your scriptb:
#!/usr/bin/python
Shebang explains to your shell, which app should process the file.
Otherwise you'll need to run it via python directly:
python myscript.py
Hi i would like to run add1.py and add2.py simultaneously and have searched BAT file and SH file but couldnt do it myself. Anyone can help me? The folder is in the following path C:\Users\Jia\Downloads\Telegram Bot\Scripts
I might also add more scripts like add3.py add4.py and the list goes on. Does anyone have simple tips that can help me run every script in this folder? Thank you!
It would be even better if the script runs one after another, example add2.py runs after add1.py finishes.
Just run: python add1.py & python add2.py. If you only want the second one to run if the first executes successfully, use python add1.py && python add2.py.
Running them at the same time would use something called concurrency, which would require some modifications to your script.
NOTE: This will only work on Windows. On Linux or MacOS, you would use: python add1.py ; python add2.py
You can manually add more scripts. To runn every python file in a folder, you could use: python *.py if you imported them all as modules into a new file called main.py and executed them in what ever order you like in that file.
As someone else already suggested, you could make a python file which executes your N python scripts.
Using subprocess as described here: https://stackoverflow.com/a/11230471/11962413
import subprocess
subprocess.call("./test1.py", shell=True)
subprocess.call("./test2.py", shell=True)
You can try this, if you are just tying to run python file:
import os
lst=[l for l in os.listdir() if l.endswith(".py")]
for ls in lst:
os.system(f'python {ls}')
Or if the name has some pattern or it is sure then, try this:
import os
for i in range(1,<up to last name+1>):
os.system(f"python add{i}.py")
So for a little context. In linux the "ifconfig" command is actually executing a "ifcfg-eth0" file found "/etc/sysconfig/network-scripts."
In windows, do command line (or powershell) commands correspond to a specific file? If so where? If it exists I having a hard time finding it.
Reason:
I am trying to execute commands from a program I am writing in Python. I know there are other ways to accomplish this ie. "import os, import subproccess." I am trying to brainstorm a simpler way to execute these commands before my program gets to heavy.
Basically I would like to tell python to execute a file ie. "ifcfg-eth0" in linux but in windows. Also, I'm just using "ipconfig" as an example There are a lot of commands I want to add.
In Windows, an easy way to find the path of a program is to use Where.exe.
where.exe ipconfig
Usually cmd commands are located in system32.
There's an executable ipconfig.exe in C:\Windows\system32
Suppose I have a file 'commands.txt' which has list of linux commands line-by-line (for example: who, pwd, ls, ps, clear etc.,)
I need a python script wherein when run should execute all the linux commands one-by-one in the console/shell.
I am looking for different approaches to do it.
May be one approach I can think off is using os module in Python. (Please correct me if I missed out anything here)
import os
with open("commands.txt") as file:
for line in file:
os.system(line)
Kindly help me with different approaches to this problem.
-TIA
Like #papey said,
Create a bash script (.sh) file, with the correct shabang (#!/usr/bin/env bash).
You could write in your Python script the following then:
import os
os.system("chmod +x myscript.sh")
os.system("./myscript.sh")
Dear fellow developers,
I'm repeatedly using (and developing) a python script for calculations, by executing it through the windows command prompt in each test.
The script has some parsed options.
In order to make each of my calculations easily reproducible, I save the actual command I entered to execute each calculation. For the moment I simply copy by hand the command once I executed it and I put it in a file. But since I have to do it for each calculation, I wonder is there is any python script line that could take my command line input, like:
python script.py --option="foo"
into a file.
The form of the command could be:
%save file=_command_used.txt% python script.py --option=foo
which would create the file and save the actual command "python script.py --option=foo" into it.
Thanks in advance!
Best regards!
I would love to have solutions for both Windows command prompt and Linux shell command prompt.
On Linux there is the script command that will capture all entered commands in a file. Use it like that:
script -a _command_used.txt
python script.py --option=foo
python script.py --option=bar
The -a option stands for append so the _command_used.txt will not be overwritten.
On Windows you can achieve a similar thing using Start-Transcript and Stop-Transcript cmdlet. See this related post.
Since you are using Python, I recommend you investigate the Xonsh shell as one way to solve this. It is cross platform and is scripted with python.