Running multiple Python scripts in different directories in a sequence - python-3.x

I am trying to run multiple experiments, which are in different folder. I want to save the results in the main folder. Something like this:
Main folder
Main_run_file.py
Results.txt
Experiment_1
Run_file.py
Experiment_2
Run_file.py
Experiment_3
Run_file.py
I already tried with the following code:
import os
mydir = os.getcwd() # would be the MAIN folder
mydir_tmp = mydir + "\Experiment_1 - 40kmh" # add the first experiment folder name
mydir_new = os.chdir(mydir_tmp) # change the current working directory
mydir = os.getcwd() # set the main directory again
import Run_file
mydir = os.getcwd() # would be the MAIN folder
mydir_tmp = mydir + "/Experiment_1 - 60kmh" # add the second experiment folder name
mydir_new = os.chdir(mydir_tmp) # change the current working directory
mydir = os.getcwd() # set the main directory again
import Run_file
However, this only runs the first Run_file and not the second one. Can someone help me with this?

The second import Run_file is ignored, since python considers this module as already imported.
Either you replace those import statements by statements like this: import Experiment_1.Run_file, without forgetting to add the __init__.py files in your subdirectories,
Or you call your python scripts with subprocess as you would do from a command-line;
from subprocess import call
call(["python", "./path/to/script.py"])
You are also missing the point about current directories:
In the second mydir = os.getcwd() # would be the MAIN folder, mydir is still the previous subfolder
Python importing system does not care about you to change the working directory: imports are managed using the python path which depends on your python installation and the directory where you run your main script.
More here: https://docs.python.org/3/reference/import.html

try out subprocess :
import subprocess
with open("Results.txt", "w+") as output:
subprocess.call(["python", "./Experiment_1/Run_file.py"], stdout=output);
subprocess.call(["python", "./Experiment_2/Run_file.py"], stdout=output);
...
if you need to pass arguments to your Run_file.py, just add it as so :
subprocess.call(["python", "./Experiment_2/Run_file.py arg1"], stdout=output);

Related

pygame.font.Font unable to read fft font file outside IDE [duplicate]

Import pygame
pygame.init()
BG = pygame.image.load('_pycache_/test_bg.jpg')
def DrawGameWin():
window.blit(BG,(0,0))
pygame.display.update()
DrawGameWin()
The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file.
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory. Alternatively, you can create an absolute file path.
The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd() and can be changed by os.chdir(path):
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
An alternative solution is to find the absolute path.
If the file is in an subfolder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()) the relative filepath. e.g.:
import pygame
import os
# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
# [...]
# join the filepath and the filename
filePath = os.path.join(sourceFileDir, 'test_bg.jpg')
# filePath = os.path.join(sourceFileDir, '_pycache_/test_bg.jpg')
surface = pygame.image.load(filePath)
The same can be achieved with the pathlib module.
Change the working directory
import os, pathlib
os.chdir(pathlib.Path(__file__).resolve().parent)
or create an absolute filepath:
import pathlib
# [...]
filePath = pathlib.Path(__file__).resolve().parent / 'test_bg.jpg'
surface = pygame.image.load(filePath)

How to extract .000 file type in python

How to extract them in python scripts by using shutil or somethings
import os
import shutil
directory = os.path.join(os.getcwd(), "BDP Raw data")
def extract(path,director,check):
if check:
shutil.unpack_archive(path, directory)
if check == False:
shutil.unpack_archive(path, directory,'tar')
def extractzip():
for file in os.listdir(directory):
if file.endswith('.zip'):
file_path = f"{directory}\{file}"
extract(file_path,directory,True)
for file in os.listdir(directory):
if file.startswith('9050') or file.startswith('9070'):
directory_path = f"{directory}\{file}"
for file in os.listdir(directory_path):
extractTo000 = f"{directory}\TestExtract000"
extract_path000 = f"{directory_path}\{file}"
extract(extract_path000,extractTo000,False)
extractzip()
Output should be like this method :
Then I must get this:
I'm not sure that shutil or even py7zr would be able to extract a .000 file.
One of the workarounds is to use Python's built-in module subprocess to unzip this kind of files by 7zip through the Windows command line.
1- CONFIGURATING THE VARIABLE ENVIRONNEMENT
First of all, you need to execute the command below in the command prompt to make sure that your user path environement is pointing to the .exe file of 7-zip. You can also do that manually (see this post).
set PATH=%PATH%;C:\Program Files\7-Zip\"
2- WRITING THE PYTHON FILE
Then, create a .py file in the same directory of your .000 files and write the code below :
from pathlib import Path
import subprocess
path = r'C:\Users\abokey\Desktop\000files'
for file in Path(path).glob('*.000'):
print(file)
subprocess.call(['7z', 'e', file], shell=True)
If you need to extract the files to a specific directory, add the option -o (Set Output Directory):
#make sure to change the output path
subprocess.call(['7z', 'e', file, r'-oC:\Users\abokey\Desktop\000files\output'], shell=True)
Make sure to adapt the code below to match your expectations :
from pathlib import Path
import os
import subprocess
directory = os.path.join(os.getcwd(), "BDP Raw data")
extractTo000 = directory + r'\TestExtract000'
for file in Path(directory).glob('*.000'):
if file.stem.startswith(('9050', '9070')):
subprocess.call(['7z', 'e', file, fr'-o{extractTo000}'], shell=True)
Note : Read full documentation of 7zip here.

Importing sounds and image files using a dictonary [duplicate]

Import pygame
pygame.init()
BG = pygame.image.load('_pycache_/test_bg.jpg')
def DrawGameWin():
window.blit(BG,(0,0))
pygame.display.update()
DrawGameWin()
The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file.
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory. Alternatively, you can create an absolute file path.
The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd() and can be changed by os.chdir(path):
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
An alternative solution is to find the absolute path.
If the file is in an subfolder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()) the relative filepath. e.g.:
import pygame
import os
# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
# [...]
# join the filepath and the filename
filePath = os.path.join(sourceFileDir, 'test_bg.jpg')
# filePath = os.path.join(sourceFileDir, '_pycache_/test_bg.jpg')
surface = pygame.image.load(filePath)
The same can be achieved with the pathlib module.
Change the working directory
import os, pathlib
os.chdir(pathlib.Path(__file__).resolve().parent)
or create an absolute filepath:
import pathlib
# [...]
filePath = pathlib.Path(__file__).resolve().parent / 'test_bg.jpg'
surface = pygame.image.load(filePath)

Relative and absolute path not read in vscode using python [duplicate]

Import pygame
pygame.init()
BG = pygame.image.load('_pycache_/test_bg.jpg')
def DrawGameWin():
window.blit(BG,(0,0))
pygame.display.update()
DrawGameWin()
The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file.
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory. Alternatively, you can create an absolute file path.
The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd() and can be changed by os.chdir(path):
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
An alternative solution is to find the absolute path.
If the file is in an subfolder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()) the relative filepath. e.g.:
import pygame
import os
# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
# [...]
# join the filepath and the filename
filePath = os.path.join(sourceFileDir, 'test_bg.jpg')
# filePath = os.path.join(sourceFileDir, '_pycache_/test_bg.jpg')
surface = pygame.image.load(filePath)
The same can be achieved with the pathlib module.
Change the working directory
import os, pathlib
os.chdir(pathlib.Path(__file__).resolve().parent)
or create an absolute filepath:
import pathlib
# [...]
filePath = pathlib.Path(__file__).resolve().parent / 'test_bg.jpg'
surface = pygame.image.load(filePath)

Using Path to check if file exists when running script outside of the directory

So I currently use Path to check if a file exists
from pathlib import Path
if Path("main.conf").is_file():
pass
else:
setup_config()
While this works as long as I'm in the directory where I'm running the script, I'd like to make it work whatever directory I'm at and just run the script. I know it doesn't work because it's expecting the main.conf to be in the directory I'm currently in but how do I tell path to only check the in the folder where the script is located in?
You can resolve the absolute path of the script by using sys.argv[0] and then replace the name of that script with the config file to check, eg:
import sys
import pathlib
path = path.Pathlib(sys.argv[0]).resolve()
if path.with_name('main.conf').is_file():
# ...
else:
# ...
Although it seems like you should probably not worry about that check and structure your setup_config so it takes a filename as an argument, eg:
def setup_config(filename):
# use with to open file here
with open(filename) as fin:
# do whatever for config setup
Then wrap your main in a try/except (which'll also cover file not exists/can't open file for other reasons), eg:
path = pathlib.Path(sys.argv[0]).resolve()
try:
setup_config(path.with_name('main.conf'))
except IOError:
pass

Resources