Running Tensorflow script from another location - python

I have created Tensorflow image classifier and image classifier should be called using another python script.
I tried os.system() but I cant just call Tensorflow scripts coz it's depends on the multiple files in the Tensorflow script location. so I have to include all the files with classifier script in the main script(2nd python script).
What is the best way to do this?
when script is running :
script error when running from another location :

Do you have tried Python Subprocess instead of os.system?
From How do you use subprocess.check_output() in Python? you can see this simple demo:
py2.py:
import sys
print sys.argv
py3.py:
import subprocess
py2output = subprocess.check_output(['python', 'py2.py', '-i', 'test.txt'])
print('py2 said:', py2output)
Running it:
$ python3 py3.py
py2 said: b"['py2.py', '-i', 'test.txt']\n"
UPDATE:
I think your problem it that you need to specify/set the correct folder path before running your python script. For example:
Consider you have the following folder structure:
/root/project/script.py
/root/project/data/file.txt
If in your python script you load a file using a relative path like ./data/file.txt, you need to run your script inside the project folder.
If you run it in the root folder, like this:
/root$ python project/script.py
the script fails, because it tries to find the data/file.txt inside the root folder.
You can use the cd command to change the current directory before running your python script. You can do it while calling the os.system, like this:
os.system("cd project && python script.py") # for my example case

whereever you call your python script from, all relative paths in your script will be based on. i.e. you called the script from its project folder in the first image, so the links to project_folder/tf_files/... were working, whereas you lateron called it from elsewhere, so that the symbolic links were messed up. your script tried to find elsewhere/tf_files/... but that subfolder does not exist.
you could edit the script so all paths are always abolute (starting with /home/...), then there is no way of confusing the

Related

Running a program using os.system in Python

I am trying to run Test.py using os.system(path) where I specify the path of the file but I am getting an error. Basically, I want to run Test.py from here and display the output.
import os
os.system(rf"C:\\Users\\USER\\OneDrive-Technion\\Research_Technion\\Python_PNM\\Sept15_2022\\220\\1\\Test.py")
The error is
The system cannot find the path specified.
you are passing a python file, not an executable. You can pass python yourfile.py.
By the way, I would reconsider what you are doing, executing a python script from another python script is quite strange.

linux command using python to go under a specific folder

Hello everyone : I have only one folder under my current directory and I want to go to it by running "cd $(ls)".
So I write this code
import os
os.system("cd $(ls)")
But this did not work for me . Anyone can help to write python syntax to go under the only available folder.
PS : the name of the folder is changeable that's why I want to use "cd $(ls)"
The OS module has some utility functions to achieve what you want to do.
os.listdir(): return a list with all files/directories inside the current working directory
os.chdir(path): changes your working directory
So, you could apply these like:
os.chdir(os.listdir()[0])
It is not obvious if by "go to it" you mean "change current directory for the remaining part of script code" or "change directory to be in after the script exits". If the later, you won't be able to do it - os.system starts a subshell, and changes of current directory in a subshell are not propagated to the parent shell. If the former, you should just use:
import glob, os
os.chdir(glob.glob('*')[0])
Use instead:
os.chdir(os.listdir('.')[0])
Although os.system("cd %(ls)) is correctly working in your shell it will not change the current working directory of your running python interpreter, because os.system() is using a separate shell instance that will be destroyed directly after the execution of the cd shell command.
Double check by executing os.getcwd() before and after (os.getcwd() returns the current working directory of your python interpreter).

Python os.chdir() not changing directory

So, I am following a simple tutorial "Python Tutorial: Automate Parsing and Renaming of Multiple Files" and am already encountering a problem where os.chdir() is not working. I am on a Windows 10 system, running python 3.6, and I have tried using both my regular terminal (which has cygwin installed) and bash on ubuntu on Windows.
Here is the code:
import os
print(os.getcwd())
os.chdir('c:/Users/Michelle Kaiser/Desktop/Lab_Progs/PI3Kalpha')
print(os.getcwd())
Here is the reg terminal:
C:\Users\Michelle Kaiser\Desktop\Lab_Progs>python rename.py
C:\Users\Michelle Kaiser\Desktop\Lab_Progs
C:\Users\Michelle Kaiser\Desktop\Lab_Progs>`
The path that it is returning corresponds to the folder my program is located in. I have moved the program 3 times to verify this. Also, it's obviously returning a path only once, so it's probably not responding to the 2 print statements.
Here is the bash terminal:
mkaiser#ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$ python rename.py
/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs
mkaiser#ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$
I also tried running the code with os.path.exists(), which did not change the output on either terminal. I have definitely double checked that I am saving my program file from one test to the next. Thanks.
I've been trying to change a file that has no whitespace.
It seems like this person has a similar problem:
Python reading whitespace-separated file lines as separate lines

Why does my python bash subprocess show a cant open file error?

I have a python tile menu script where a subprocess executes a bash shell:
# excute shell script
subprocess.call([self.path + '/pimenu.sh'] + actions)
and in the shell I have:
python ./to/file/name/"$*".py
but when the python script which is found and executed returns an error as it cant find the folder with the images in.:
pygame.error: Couldn't open ./file/name/image.jpg
I am assuming it is looking in the folder the menu script is in, how can I give python or bash the correct path to the scripts resources?
You can use a path finding built in python's os package into the scripts ./to/file/name/"$*".py like this:
import os
print os.getcwd()
It is good enought described into the next SO answer: https://stackoverflow.com/a/3430395/2261861

Python : get all exe files in current directory and run them?

First of all this is not homework, I'm in a desperate need for a script that will do the following, my problem is, I've never had to deal with python before so I barely know how to use it - and I need it to launch unit tests in TeamCity via a commandline build runner
What I need exactly is :
a *.bat file that will run the script
a python script that will :
get all *_test.exe files in the current working directory
run all the files which were the result of the search
Best regards
import glob, os
def solution():
for fn in glob.glob("*_text.exe"):
os.startfile(fn)
If you copy this into a file, the script should do as you asked.
import os # Access the operating system.
def solution(): # Create a function for later.
for name in os.listdir(os.getcwd()):
if name.lower().endswith('_test.exe'):
os.startfile(name)
solution() # Execute this inside the CWD.

Categories

Resources