Python run script in same directory

How can I run a batch file with Python (in the same directory as the Python scripts)?

Note that the directory cannot be constant as it can be changed from one user to another.

Mike Müller

79.3k18 gold badges157 silver badges159 bronze badges

asked Jan 12, 2017 at 22:04

Python run script in same directory

3

You need to find out where your script is and assemble an absolute path:

import os
import subprocess

dirname = os.path.dirname(os.path.abspath(__file__))
cmd = os.path.join(dirname, 'mybatch_file')

subprocess.call(cmd)

In Steps

You can find out the name of script with:

__file__

Now make it an absolute path:

os.path.abspath

and get the directory it is in:

os.path.dirname

Finally, join this path with your batch file name:

os.path.join

before you feed it to:

subprocess.call

answered Jan 14, 2017 at 10:08

Mike MüllerMike Müller

79.3k18 gold badges157 silver badges159 bronze badges

you can achieve it use subprocess module

from subprocess import call
comando = 'path_to_the_script'
call(comando, shell=True)

answered Jan 12, 2017 at 22:24

Python run script in same directory

In this guide, you’ll see how to run one Python script from another Python script.

More specifically, you’ll see the steps to:

  • Run one Python script from another
  • Call a specific variable from one Python script to another

But before we begin, here is a simple template that you may use to run one Python script from another (for Python scripts that are stored in the same folder):

import script_name_to_call

Step 1: Place the Python Scripts in the Same Folder

To start, place your Python scripts in the same folder.

For example, let’s suppose that two Python scripts (called python_1 and python_2) are stored in the same folder:

python_1
python_2

The ultimate goal is to run the python_2 script from the python_1 script.

Step 2: Add the Syntax

Next, add the syntax to each of your scripts.

For instance, let’s add the following syntax in the python_1 script:

import python_2
print('what are you up to?')

Where:

  • The first line of ‘import python_2’ in the python_1 script, would call the second python_2 script
  • The second line of the code simply prints the expression of ‘what are you up to?’

Now let’s add the syntax in the python_2 script:

print('hello world')

In this case, the expression of ‘hello world’ would be printed when running the second script.

Note that you must first save the syntax that was captured in the python_2 script before calling it from another script.

Step 3: Run One Python Script From Another

Now you’ll need to run the script from the python_1 box in order to call the second script.

Notice that the results of the python_2 script would be displayed first, and only then the results of the python_1 script would be displayed:

hello world
what are you up to?

Call a Specific Variable from One Python Script to Another

Let’s now see how to call a specific variable (which we will call ‘x’) from the python_2 script into the python_1 script.

In that case, you’ll need to edit the syntax in the python_1 script to the following:

import python_2 as p2
print(p2.x)

Next, assign a value (e.g., ‘hello world’) to the ‘x’ variable in the python_2 script:

x = 'hello world'

Don’t forget to save the changes in the python_2 script.

Finally, run the syntax from the python_1 script, and the ‘hello world’ expression would be printed:

hello world

Interaction of Variables from the Two Scripts

In the final section of this guide, you’ll see how variables from the two scripts may interact.

For example, let’s suppose that the python_1 script has the variable of y = 2, while the python_2 script has the variable of x = 5. The goal is to sum those two variables and display the results.

First, modify the syntax in the python_1 script to the following:

import python_2 as p2
y = 2
print(p2.x + y)

Then, change the syntax in the python_2 script to:

x = 5

As before, don’t forget to save the changes in the python_2 script.

Finally, run the syntax from the python_1 script, and you’ll get ‘7’ which is indeed the sum of the two variables:

7

How do I run a Python script in the same folder?

Steps to Run One Python Script From Another.
Step 1: Place the Python Scripts in the Same Folder. To start, place your Python scripts in the same folder. ... .
Step 2: Add the Syntax. Next, add the syntax to each of your scripts. ... .
Step 3: Run One Python Script From Another..

How do I open files in the same folder?

You may the use the following to open up files in the same folder: f = open(os. path..
And for the full path of the file: os. path. ... .
This answer to the first question, not the one after the EDIT. – mcoolive. ... .

How do I run a py file from another py file?

Use the execfile() Method to Run a Python Script in Another Python Script. The execfile() function executes the desired file in the interpreter. This function only works in Python 2. In Python 3, the execfile() function was removed, but the same thing can be achieved in Python 3 using the exec() method.

What is Runpy py?

Source code: Lib/runpy.py. The runpy module is used to locate and run Python modules without importing them first. Its main use is to implement the -m command line switch that allows scripts to be located using the Python module namespace rather than the filesystem.