The easiest answer is probably to change your working directory, then call the second .py file from where it is:
python a.py && cd testA && python ../b.py
Of course you might find it even easier to write a script that does it all for you, like so:
Save this as runTests.sh in the same directory as a.py is:
#!/bin/sh
python a.py
cd testA
python ../b.py
Make it executable:
chmod +x ./runTests.sh
Then you can simply enter your directory and run it:
./runTests.sh
Answer from 3D1T0R on Stack OverflowThe easiest answer is probably to change your working directory, then call the second .py file from where it is:
python a.py && cd testA && python ../b.py
Of course you might find it even easier to write a script that does it all for you, like so:
Save this as runTests.sh in the same directory as a.py is:
#!/bin/sh
python a.py
cd testA
python ../b.py
Make it executable:
chmod +x ./runTests.sh
Then you can simply enter your directory and run it:
./runTests.sh
I managed to get b.py executing and producing the testB folder where I need it to, while remaining in the MAIN folder. For anyone who might wonder, at the beginning of my b.py script I would simply use mydir = os.getcwd() which normally is wherever b.py is.
To keep b.py in MAIN while making it work on files in other directories, I wrote this:
mydir = os.getcwd() # would be the MAIN folder
mydir_tmp = mydir + "//testA" # add the testA folder name
mydir_new = os.chdir(mydir_tmp) # change the current working directory
mydir = os.getcwd() # set the main directory again, now it calls testA
Running the bash script now works!
This question has been absolutely beat to death. I know this because I spent quite some time googling it and not two answers are the same. I think this is partly because there are many use cases and the language / tech specific term barriers for us noobs. It's seriously infuriating!
So, I'll try to explain my use case. I simply want to be able to run my .py scripts from PowerShell (on win10) in other directories than where the script resides. This is because I want to do things with the files of those other dirs, while getting information from PowerShell console.
I know one solution is to compile into executables and make a batch script, but I wonder if there's a way to avoid having to go through that every time I make a little change to the script. Also it seems parameters does not work this way and console output is not visible (or it opens and closes).
I'm open to any suggestions!
Thanks for reading my question / rant :)
Run python script in subfolder
windows - Run python script from another directory - Stack Overflow
Run Python Script From Script Directory/Current Directory - Stack Overflow
run python scripts from anywhere in the filesystem in windows - Stack Overflow
Videos
import os
from os.path import abspath, dirname
os.chdir(dirname(abspath(__file__)))
You can use dirname(abspath(__file__))) to get the parent directory of the python script and os.chdir into it to make the script run in the directory where it is located.
The easiest answer is probably to change your working directory, then call the .py file from where it is:
cd path/to/python/file && python ../.py
Of course you might find it even easier to write a script that does it all for you, like so:
Save this as runPython.sh in the directory where you're running the python script from, is:
#!/bin/sh
cd path/to/python/file
python ../script.py
Make it executable (for yourself):
chmod +x ./runPython.sh
Then you can simply enter your directory and run it:
./runPython.sh
If you want to only make changes to the python script:
mydir = os.getcwd() # would be the folder where you're running the file
mydir_tmp = "path/to/python/file" # get the path
mydir_new = os.chdir(mydir_tmp) # change the current working directory
mydir = os.getcwd()
The reason you got an error was because sys.argv[0] is the name of the file itself, instead you can pass the directory as a string and use sys.argv[1] instead.
Make sure your
Python 3.7is added to System Environment path Variable. it is pretty easy like how you addJREorMAVEN. Just follow this question but do it for Python.Open CMD in any arbitrary path and type
python --version. it must provide you the version of your python. Something likePython 3.7.4In the navbar of the folder that your file exist, write CMD and hit Enter. It must open a cmd for you and find the test.py. To make sure, your cmd is in correct path, please type
dirand hit enter. It must show the name of your file.If it was fine, then you can safely call
python test.pyand it runs your code.
cd to directory containing python script
cd C:/Desktop/Folder
On command line, type
python script_name.py
(on Windows 10)
I made a Python script and I'd like to be able to be in any folder and run it in the powershell. I tried adding the folder where it's located to the path but it doesn't work.