0

I have a bash script which changes the path on my command line, This one,

#!/usr/bin/env python
cd /mnt/vvc/username/deployment/

I have a python script which i wish to run after the path changes to the desired path,

The script,

#!/usr/bin/env python
import subprocess
import os
subprocess.call(['/home/username/new_file.sh'])

for folder in os.listdir(''):
print ('deploy_predict'+' '+folder)

I get this

 File "/home/username/new_file.sh", line 2
 cd /mnt/vvc/username/deployment/
                                ^
SyntaxError: invalid syntax

Any suggestions on how can i fix this?thanks in advance

6
  • 1
    Your script's syntax might be bash syntax, but you're telling your OS to run it using /usr/bin/env python (meaning: to run it interpreted as Python). I'd change the line #!/usr/bin/env python to #!/bin/bash Commented Jan 4, 2018 at 5:26
  • @BorrajaX Thanks,The error is gone,but the script doesn't change the path,on the command line it is still in the home directory Commented Jan 4, 2018 at 5:29
  • 1
    Subprocess spawns a different process than the terminal you're on (a brand new process). So you do cd in the spawned process, not in your terminal (not in your parent process) You can maybe understand it as if it opened another terminal? (dunno if that may confuse you more, though) Commented Jan 4, 2018 at 5:31
  • A little confused,but how do i get it to change path on my terminal using this script? Commented Jan 4, 2018 at 5:33
  • 1
    I really doubt that's what you wanna do... In any case, that'd depend on which terminal you're using and if it offers some kind of method to externally changing the directory. What is it you want to exaclty do? Usually, you cd in your spawned process, run whatever you need to run and capture its output Commented Jan 4, 2018 at 5:35

1 Answer 1

3

You need to explicitly tell subprocess which shell to run the sh file with. Probably one of the following:

subprocess.call(['sh', '/home/username/new_file.sh'])
subprocess.call(['bash', '/home/username/new_file.sh'])

However, this will not change the python program's working directory as the command is run in a separate context.

You want to do this to change the python program's working directory as it runs:

os.chdir('/mnt/vvc/username/deployment/')

But that's not really great practice. Probably better to just pass the path into os.listdir, and not change working directories:

os.listdir('/mnt/vvc/username/deployment/')
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Daniel, the error dissapears but it doesn't change path on the command line
You can't change the python program's working directory like that. The command executes in an external context. Just pass in the directory to os.listdir('/mnt/vvc/username/deployment/') and get rid of all that subprocess stuff.
Or you can use os.chdir('/mnt/vvc/username/deployment/') to change the python working directory
But i have to run a file called deploy_predict on the terminal after the path change which takes in folder as the argument,if i do os.listdir,i wont be able to run it,
You need to update your original question with info about this deploy_predict nonsense, I have no idea what you're trying to get across.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.