1

I have a file with a .run extension, e.g. myfile.run. It is executable, i.e. the permission level has been correctly set.

In a Linux terminal, I can execute it simply by submitting ./myfile.run.

In Python 3.6, I tried executing this same file using the subprocess.run() function but have not been successful. :( I tried:

result = subprocess.run( ['./home/user1/myfile.run'], stdout=subprocess.PIPE )
Traceback (most recent call last):
  File "<pyshell#51>", line 1, in <module>
    result = subprocess.run( [a], stdout=subprocess.PIPE )
  File "/usr/lib/python3.6/subprocess.py", line 423, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
NotADirectoryError: [Errno 20] Not a directory: './home/user1/myfile.run'

I tried:

result = subprocess.run( ['/home/user1/myfile.run'], stdout=subprocess.PIPE )

and

result = subprocess.run( '/home/user1/myfile.run', stdout=subprocess.PIPE, shell=True ) 

Nothing happened.

What is the correct subprocess.run() syntax that I should use? Thanks.

21
  • What do you mean "nothing happened"? Does myfile.run output things, and have you checked result? Commented Jul 30, 2019 at 10:29
  • @wizzwizz4 There were no error msg. The file is suppose to copy/install a bunch of file to another directory. But nothing happened? Commented Jul 30, 2019 at 10:33
  • what os.stat(path) says ? Commented Jul 30, 2019 at 10:33
  • 1
    @wizzwizz4 The solution on How do I change directory cd in Python? is dated. subprocess.run() in Python 3.6 has the cwd keyword argument. Therefore the os.chdir() command is not required to solve the wrong directory issue. The correct approach is to set the value of the cwd keyword argument of subprocess.run() . Appreciate you remove your comment that this is a duplicate question as it isn't. Commented Jul 31, 2019 at 14:29
  • 1
    The shebang would go at the top of the Bash script which you are trying to run in a subprocess. This is basic Unix 101 so it really has been covered before, hundreds of times. Commented Jul 31, 2019 at 16:38

1 Answer 1

1

The correct syntax to using subprocess.run() to execute a .run executable file is:

result = subprocess.run( ['/bin/bash', '/home/user1/myfile.run'], stdout=subprocess.PIPE )

Essentially, the .run file is a bash script, and to run a bash file I needed to activate /bin/bash. The . symbol commonly used to run a bash script in a terminal could not work in subprocess.run(). Hence, /bin/bash had to be declared explicitly.

After solving my question, I discovered that the data created by the myfile.run bash script was written to a sub-directory of the directory holding my python script which submitted the subprocess.run() function. However, this outcome was wrong. The data should have been written into a sub-directory of the directory holding the myfile.run bash script; this was the outcome when ./home/user1/myfile.run was submitted in a terminal.

A common view on how to change the current working directory in python is to use the function os.chdir() (see here). However, I discovered that the function subprocess.run() in Python 3.6 has a current working directory keyword argument kwarg called cwd=None. Hence, to resolve my wrong directory issue (as described above), I simply had to equate the working directory that I want with cwd, e.g. cwd='/home/user1/Project'. Meaning, the use of os.chdir() with subprocess.run() function is unnecessary and avoided.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.