How to goto/access home directory using python subprocess.run()Actual meaning of 'shell=True' in subprocessHow do I copy a file in Python?How can I safely create a nested directory?How to get the current time in PythonHow can I make a time delay in Python?How do I get the number of elements in a list?How do I concatenate two lists in Python?How do I list all files of a directory?How to get the home directory in Python?How to access environment variable values?How to find if directory exists in Python

Wrong Schengen Visa exit stamp on my passport, who can I complain to?

Would it be unbalanced to increase a druid's number of uses of Wild Shape based on level?

How do we know that black holes are spinning?

Shouldn't countries like Russia and Canada support global warming?

How to publish superseding results without creating enemies

Reordering of matrix multiplication

What is the mathematical notation for rounding a given number to the nearest integer?

How to write characters doing illogical things in a believable way?

Block diagram vs flow chart?

What would happen if Protagoras v Euathlus were heard in court today?

In what sequence should an advanced civilization teach technology to medieval society to maximize rate of adoption?

Output a Super Mario Image

Why is it called stateful and stateless firewall?

Are there any “Third Order” acronyms used in space exploration?

A Mainer Expression

Why is this sentence grammatical?

Why is belonging not transitive?

Permutations in Disguise

Why don't airports use arresting gears to recover energy from landing passenger planes?

How much would a 1 foot tall human weigh?

Examples of proofs by making reduction to a finite set

Is the Dodge action perceptible to other characters?

Are there objective criteria for classifying consonance v. dissonance?

Amortized Loans seem to benefit the bank more than the customer



How to goto/access home directory using python subprocess.run()


Actual meaning of 'shell=True' in subprocessHow do I copy a file in Python?How can I safely create a nested directory?How to get the current time in PythonHow can I make a time delay in Python?How do I get the number of elements in a list?How do I concatenate two lists in Python?How do I list all files of a directory?How to get the home directory in Python?How to access environment variable values?How to find if directory exists in Python






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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)









share|improve this question



















  • 1





    what do you plan to do after that ?

    – Kunal Mukherjee
    Mar 28 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 at 12:41


















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)









share|improve this question



















  • 1





    what do you plan to do after that ?

    – Kunal Mukherjee
    Mar 28 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 at 12:41














0












0








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)









share|improve this question














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)






python subprocess






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 12:19









Sun BearSun Bear

2,0991 gold badge14 silver badges38 bronze badges




2,0991 gold badge14 silver badges38 bronze badges










  • 1





    what do you plan to do after that ?

    – Kunal Mukherjee
    Mar 28 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 at 12:41













  • 1





    what do you plan to do after that ?

    – Kunal Mukherjee
    Mar 28 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 at 12:41








1




1





what do you plan to do after that ?

– Kunal Mukherjee
Mar 28 at 12:22





what do you plan to do after that ?

– Kunal Mukherjee
Mar 28 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 at 12:41






@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 at 12:41













2 Answers
2






active

oldest

votes


















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)





share|improve this answer



























  • Must I declare home = os.environ['HOME'] in each python class or at each python module?

    – Sun Bear
    Mar 28 at 12:27











  • you can use directly os.environ['HOME'] e.g. os.chdir(os.environ['HOME'])

    – dzang
    Mar 28 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 at 12:31












  • You need to call bash explicitly, see edit

    – dzang
    Mar 28 at 12:40











  • 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 at 12:54



















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






share|improve this answer



























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55397477%2fhow-to-goto-access-home-directory-using-python-subprocess-run%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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)





    share|improve this answer



























    • Must I declare home = os.environ['HOME'] in each python class or at each python module?

      – Sun Bear
      Mar 28 at 12:27











    • you can use directly os.environ['HOME'] e.g. os.chdir(os.environ['HOME'])

      – dzang
      Mar 28 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 at 12:31












    • You need to call bash explicitly, see edit

      – dzang
      Mar 28 at 12:40











    • 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 at 12:54
















    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)





    share|improve this answer



























    • Must I declare home = os.environ['HOME'] in each python class or at each python module?

      – Sun Bear
      Mar 28 at 12:27











    • you can use directly os.environ['HOME'] e.g. os.chdir(os.environ['HOME'])

      – dzang
      Mar 28 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 at 12:31












    • You need to call bash explicitly, see edit

      – dzang
      Mar 28 at 12:40











    • 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 at 12:54














    0














    0










    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)





    share|improve this answer















    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)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 28 at 13:11

























    answered Mar 28 at 12:24









    dzangdzang

    6093 silver badges10 bronze badges




    6093 silver badges10 bronze badges















    • Must I declare home = os.environ['HOME'] in each python class or at each python module?

      – Sun Bear
      Mar 28 at 12:27











    • you can use directly os.environ['HOME'] e.g. os.chdir(os.environ['HOME'])

      – dzang
      Mar 28 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 at 12:31












    • You need to call bash explicitly, see edit

      – dzang
      Mar 28 at 12:40











    • 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 at 12:54


















    • Must I declare home = os.environ['HOME'] in each python class or at each python module?

      – Sun Bear
      Mar 28 at 12:27











    • you can use directly os.environ['HOME'] e.g. os.chdir(os.environ['HOME'])

      – dzang
      Mar 28 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 at 12:31












    • You need to call bash explicitly, see edit

      – dzang
      Mar 28 at 12:40











    • 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 at 12:54

















    Must I declare home = os.environ['HOME'] in each python class or at each python module?

    – Sun Bear
    Mar 28 at 12:27





    Must I declare home = os.environ['HOME'] in each python class or at each python module?

    – Sun Bear
    Mar 28 at 12:27













    you can use directly os.environ['HOME'] e.g. os.chdir(os.environ['HOME'])

    – dzang
    Mar 28 at 12:28





    you can use directly os.environ['HOME'] e.g. os.chdir(os.environ['HOME'])

    – dzang
    Mar 28 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 at 12:31






    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 at 12:31














    You need to call bash explicitly, see edit

    – dzang
    Mar 28 at 12:40





    You need to call bash explicitly, see edit

    – dzang
    Mar 28 at 12:40













    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 at 12:54






    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 at 12:54














    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






    share|improve this answer





























      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






      share|improve this answer



























        0














        0










        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






        share|improve this answer













        @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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 8 at 6:34









        Ashutosh RainaAshutosh Raina

        11 bronze badge




        11 bronze badge































            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55397477%2fhow-to-goto-access-home-directory-using-python-subprocess-run%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

            밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

            1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴