-3

Ι need to change the paths using subprocess but it doesn't work

I am using py3.6 - python-telegrambot

it's my code

def sh(bot,update):
  ID = 289444284
  command=update.message.text
  print(command)
  su = subprocess.getstatusoutput(command)
  bot.sendMessage(ID,su[1] )

and also

subprocess.Popen(command, shell=True, stdout=PIPE).communicate()
subprocess.getoutput(command)
os.system(command)

But when I put the cd command back, I'm on the same path and the path does not change

![enter image description here][1]

[1]: screen shot

SasaN AndeH
  • 23
  • 1
  • 5

1 Answers1

0

When you call any of the above (subprocess.* or os.system()) you create a new (child) process if you run cd this way, you change current working directory only for the cd itself, but it does not affect your running python code and any command you execute afterwords. You'd need to use os.chdir() or just hold that information and pass it into newly created children processing with cwd keyword of subprocess.Popen().

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
  • 1
    In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section "Answer well-asked questions", and the bullet within regarding questions which "*have already been asked and answered many times before*". – Charles Duffy Feb 03 '18 at 18:56