-1

I want to run a shell command in python. In shell i am doing something like this

cd project_name
ls

output: app1 app2

cd app1

I want to do the same thing in python with os module

import os
os.system('cd project_name') #It goes into the folder project_name but when i run ls command it is showing same path.
os.system('ls')
os.sytem('cd app1') # No such file or directory

Please tell me how i can i do the same. Thanks in advance.

sam
  • 203
  • 1
  • 3
  • 15
  • Duplicate of [How do I change the working directory in Python?](https://stackoverflow.com/questions/431684/how-do-i-change-the-working-directory-in-python) – oguz ismail Jun 20 '20 at 06:37
  • 1
    Why change the directory and why use the shell for things done in python? `os.listdir("project_name")` does the first two things. – tdelaney Jun 20 '20 at 06:41

2 Answers2

0

Each time you run os.system() you create a new subshell that is seperate from an previous os.system() calls.

If you want to run multiple commands you can string them together with

os.system("cd projectname && ls && cd app1")

Alternatively you can change directory in the subshell with

os.chdir(path)
7pixels
  • 51
  • 4
0

You can keep track of paths in local variables and use them as needed. pathlib offers a convenient wrapper class for this.

>>> from pathlib import Path
>>> project_path = Path("project_name")
>>> project_path
PosixPath('project_name')
>>> str(project_path)
'project_name'

You can list

>>> print(list(project_path.iterdir()))
[PosixPath('project_name/app1'), PosixPath('project_name/app2')]

And make subpaths

>>> app1_path = project_path.joinpath("app1")
>>> app1_path
PosixPath('project_name/app1')
>>> str(app1_path)
'project_name/app1'

You can still run commands with os.system (notice the semicolon separating two commands)

>>> import os
>>> rc = os.system(f"cd {app1_path};ls")
foo  some_file

or use the subprocess module which allows you to specifiy current working directory

>>> import subprocess as subp
>>> rc = subp.run("ls", shell=True, cwd=app1_path)
foo  some_file

Code that unilaterally changes path can be hard to read. And its easy to get lost and do work in the wrong directory.

tdelaney
  • 73,364
  • 6
  • 83
  • 116