HI guys i am new to scripting, i am doing small project in python and wxpython, which does automation. Now i have a problem while sending variable from python to shell script.
I have to connect ftp ,then download a file which is written in BASH(1st script). That bash script call one more bash script(2nd script) which is also in ftp server. BUT the 2 script name is not hard-coded in 1st script. so i have to send that "script name" from my python script which is i am not able to do right now.
so i need yours help to solve my issue, have gone through google but couldn't find anything.
Thanks in advance.
I have tried directly to run this but still it don't take 2nd argument,so i have gone through the script once. found that,
when $2 $3 $7 already configured inside the 1st script (those are in built in myproject.sh)
please help me to solve this issue..
The quickest and easiest way is to let your Python script create a file that sets a variable name using bash syntax, and then let your first bash script source it. Sourcing it will basically set that variable.
So in Python, you want to output that file somewhere:
name_of_script="/path/to/second/script.sh"
with open("/my/path/scriptnamevar.sh", "w") as f:
#Output to bash-recognized variable setting syntax
f.write("SCRIPT_NAME={}".format(name_of_script))
Then in your first script, you just need to source scriptnamevar.sh (or whatever you decide to call it).
source /my/path/scriptnamevar.sh
# This should output whatever name_of_script was in python
echo $SCRIPT_NAME
You could make use of command line arguments. Since you're using subprocess to call the first bash script, you could modify it to look like this:
subprocess.call(["bashScript1Name", "bashScript2Name"])
Then in bashScript1Name the name of the second script will be in the variable $1
Related
I will try to be brief!
For whatever reason, I couldn't make the entirety of my program in python, so I had to outsource one specific task to php (a language I do not know very well). As the python program runs, it is supposed to trigger the php program to run, and then do a few things afterwards which is not a problem.
It seems to me that, to be able to run something through python, you need it to be able to run through cmd first, and then you can make python use cmd to run the program. I had a few issues there, because the programs are on different drives, and the php program references other files and locations in the same directory and in sub-directories to where it is, this means I couldn't execute in one line of cmd, but first had to change directory, to then execute the php program from the folder it's in. Because my command wasn't just one line, I made a batch file containing all the steps.
My current working method is to open up cmd, change directory in cmd to where the php file is, and then run the php file. I had to add php to the "Environment Variable Path" to be able to do this. Here is the batch file that currently works when run by me:
cd /d C:
cd C:\Users\UserMain\Desktop\php\colorextract
php (2).php
When I double click this bat file, from my E drive, it successfully executes the php program. But when I tell python to execute the batch file, that is where things go wrong.
Here is my python code, apologies for the name of the bat file:
import os
os.system('cmd /k "bitch.bat"')
The resultant cmd window then goes thru the steps of the batch file: 1) it changes to the right directory, 2) it is unable to execute the php file because:
'php' is not recognised as an internal or external command, operable program or batch file.
Now, this is the standard error you get if you were to try running a php program without having added php to the "Environment Variable Path", I know this because I went through that same thing. But if I manually open a cmd window, not administrative or anything, I can 1) successfully perform the steps outlined in batch file, and program runs, and 2) I can even run the bat file, and that also runs the program.
The cmd window opened by python does not seem to be able to reference the "Environment Variable Path", or it is for another reason somehow handicapped against being able to do all the things that a normal cmd widow can. How can this be fixed?
Thanks in advance to anyone who reads this!
Edit: I found that python had not detected the changes I made to the environment variables the day before, hence why python's cmd was giving the exact error that not having php in the environment variable gives. After I restarted my computer, my code worked. Thank you to #Gerhard and #Aaron Junker for making me think much harder about this issue.
so I found a command that can be run after importing os.
print(os.environ)
I ran this, and it told me that Python could not see that php had been added to the environment variables, well, more likely that python did not have the most up to date information regarding what was in the path variable(s).
Restarting my computer made the changes kick in, and now my original code works. Whilst I do feel very stupid, I'm just happy that this is resolved.
This seems to me like both instances use different environment variables.
Open
System Properties -> Advanced -> environment variables and look that PHP is in the PATH variable in user variables and in System variables.
I have one Maya scene and a Python script where import obj files into it. I need to create a batch render file which calls the maya file and applies the script without opneing maya.
I have this code in a .sh file:
#!/bin/bash
"/Applications/Autodesk/maya2016/Maya.app/Contents/bin/Render" -r file -s 1 -e 4 -cam camera1 -rd "/Users/MyComp/Documents/maya/projects/default/images" "/Users/MyComp/Documents/maya/projects/default/Scenes/test1.mb"
But I have this code into the script which can be an issue or maybe not:
def renderFile(i):
cmds.setAttr("defaultRenderGlobals.imageFilePrefix", i, type="string")
cmds.render(batch=True)
If I execute this .sh file it renders without the python script. How can I add the python script?
I need that file for a renderfarm purposes
I know it's an old thread but thought I'd jump in just incase someone finds this thread in a search.
The comments seem a little confused. This comes from the fact that there are two different Python interpreters being talked about. The first is the system level one, which the original question seems to be talking about. In that case, you can use any of the various shell command launchers (like, subprocess/Popen) that suit your need. Here you are looking to run the render command like you would any other command in in the shell.
In the responses, people there are referring to the other interpreter, the custom Maya Python interpreter (mayapy.exe). In that case you are working with actual Maya libraries and it's the same as working with Python in it's shell, with the added Maya libraries/environment.
The two have different uses, the first is to control things like they were in the shell and the second is controlling things inside of a Maya context. Hope that clarifies things.
I am new to Python, could someone please let me know if we can register our python script to work as one of the command of cmd.exe, I am not asking about parsing or accepting command line arguements using argparse, etc..
Lets say I have a SearchDir.py file, to run it from cmd prompt I have to do "python SearchDir.py", so is there any way to simply do "SearchDir" and it should act as one of the command of cmd.exe
Yes, it is possible (if I have understood you correctly. The question was a bit unclear)
The first thing you need to do is to add a shebang to the top of your script: https://en.wikipedia.org/wiki/Shebang_(Unix). I see you mentioned cmd.exe so I assume you need this to work on Windows? In that case you should read https://docs.python.org/3/using/windows.html#shebang-lines as well
On unix hosts, the second thing we need to do is set our file as executable with chmod +x <filename>. I try to stay away from Windows, but from what I remember this is not relevant on Windows.
Last you need to place the script in a folder referenced by your $PATH-variable
The script will now be accessible as SearchDir.py from cmd.exe or a unix shell. If you want to omit the ".py"-part you simply rename the file to just "SearchDir"
I am using python3.5 to learn how to scrape data from website. And I realize IDLE is slow when the input explodes (like when I using .text to check the contents of the web). So I use Bash to test my scraper.py script.
After I enter python in Bash:
154-76:~ FDSM_lhn$ python3.5
It's hard for me to open a .py file. The only way I know how to do that is:
import scraper.py
which is not convenient because the object I create isn't in that environment. During the test I need to check something in it from time to time.
Can anyone can help me fix this?
If you have a file called scraper.py, you should be able to execute it via python -i scraper.py. This will leave it interactive.
I am very new to Python and I have been trying to find a way to write in cmd with python.
I tried os.system and subprocess too. But I am not sure how to use subprocess.
While using os.system(), I got an error saying that the file specified cannot be found.
This is what I am trying to write in cmd os.system('cd '+path+'tesseract '+'a.png out')
I have tried searching Google but still I don't understand how to use subprocess.
EDIT:
It's not a problem with python anymore, I have figured out. Here is my code now.
os.system("cd C:\\Users\\User\\Desktop\\Folder\\data\\")
os.system("tesseract a.png out")
Now it says the file cannot be open. But if I open the cmd separately and write the above code, it successfully creates a file in the folder\data.
Each call to os.system is a separate instance of the shell. The cd you issued only had effect in the first instance of the shell. The second call to os.system was a new shell instance that started in the Python program's current working directory, which was not affected by the first cd invocation.
Some ways to do what you want:
1 -- put all the relevant commands in a single bash file and execute that via os.system
2 -- skip the cd call; just invoke your tesseract command using a full path to the file
3 -- change the directory for the Python program as a whole using os.chdir but this is probably not the right way -- your Python program as a whole (especially if running in a web app framework like Django or web2py) may have strong feelings about the current working directory.
The main takeaway is, os.system calls don't change the execution environment of the current Python program. It's equivalent to what would happen if you created a sub-shell at the command line, issued one command then exited. Some commands (like creating files or directories) have permanent effect. Others (like changing directories or setting environment variables) don't.