0

While using python3 subprocess.run(), I need to got/access my home directory. I tried the below commands but their syntax is incorrect. Please advice me on the correct syntax that I should use. Thank you.

Test Script:

import subprocess as sp
sp.run(['cd', '$HOME'], stdout=sp.PIPE, stderr=sp.PIPE)
sp.run(['cd', '${HOME}'], stdout=sp.PIPE, stderr=sp.PIPE)
sp.run(['cd', '~'], stdout=sp.PIPE, stderr=sp.PIPE)
Sun Bear
  • 7,594
  • 11
  • 56
  • 102
  • 1
    what do you plan to do after that ? – Kunal Mukherjee Mar 28 '19 at 12:22
  • @KunalMukherjee `cd` is just a test cmd. In reality, I need to run some program in bash that references the home directory. I am trying to get the referencing syntax for home dir correct. Thanks for asking. – Sun Bear Mar 28 '19 at 12:41

2 Answers2

0

You can use os.environ:

home = os.environ['HOME']
import subprocess as sp
sp.run(['bash', 'cd', home], stdout=sp.PIPE, stderr=sp.PIPE)

This will not change your python interpreter working directory, for that purpose you might want to use:

home = os.environ['HOME']
os.chdir(home)

If you need to access subdirectories you can get the paths using os.path.join:

home = os.environ['HOME']
subdir = 'Documents'  # or get the list of subdirs with os.listdir(home)
subdir_path = os.path.join(home, subdir)
dzang
  • 2,160
  • 2
  • 12
  • 21
  • Must I declare `home = os.environ['HOME']` in each python class or at each python module? – Sun Bear Mar 28 '19 at 12:27
  • you can use directly `os.environ['HOME']` e.g. `os.chdir(os.environ['HOME'])` – dzang Mar 28 '19 at 12:28
  • I got this error msg: `FileNotFoundError: [Errno 2] No such file or directory: 'cd': 'cd'`. How to fix this? I am using python 3.6. – Sun Bear Mar 28 '19 at 12:31
  • Thanks. How do I access/reference the children directories of `home`? E.g. `home/Document` or `~/Document`. I could not get it to work. – Sun Bear Mar 28 '19 at 12:54
  • Pardon me. After implementing your latest edit, I ran `sp.run(['bash', 'cd', subdir_path], stdout=sp.PIPE, stderr=sp.PIPE)`. However, I got an error. `CompletedProcess(args=['bash', 'cd', '~/Documents'], returncode=127, stdout=b'', stderr=b'bash: cd: No such file or directory\n')`. The directory does exist. Can you pls advise me again? – Sun Bear Mar 28 '19 at 14:23
  • you shouldn't use `~`, because that's a bash shortcut. You should use `os.path.join(os.path.environ['HOME'], 'Documents')`. Please select this answer as accepted, since it answers your original question. Now if you have further problems, please post a new question with the full code, so that you can get help, instead of posting questions in the comments about errors you get, without showing the code that generated them, because we cannot help properly without the code... Thanks – dzang Mar 28 '19 at 14:46
  • Apologies for the confusion. I did not use `~`. However, I changed the `/home/user/` syntax to `~` in my comment. So I had ran `os.path.join(os.path.environ['HOME'], 'Documents')` but it returned `stderr=b'bash: cd: No such file or directory\n`. – Sun Bear Mar 28 '19 at 17:17
  • Can you add the full code, together with the output of `os.listdir(os.environ['HOME'])`? – dzang Mar 28 '19 at 19:29
0

@SunBear Try adding shell=True. It was not working earlier for you because subprocess can't find the test-cmd cd.

To understand more about shell=True you can go here Actual meaning of 'shell=True' in subprocess