-1

I edited this post so that i could give more info about the goal I am trying to achieve. basically I want to be able to open VSCode in a directory that I can input inside a python file I am running trhough a shell command i created. So what I need is for the python file to ask me for the name of the folder I want to open, pass that information to the terminal so that it can then cd into that folder and open vscode automatically. I tried with os.system() that is, as I read, one of the ways I can achieve that goal. The problem is that if I use standard commands like os.system('date') or os.system('code') it works without any problem. If I try to use os.system(cd /directory/) nothing happens. As suggested I also tryied with subprocess.call(["cd", "/home/simon/Desktop"]) but the terminal gives me the error: FileNotFoundError: [Errno 2] No such file or directory: 'cd' I am going to include both the python file:

import os, subprocess

PATH = "/home/simon/Linux_Storage/Projects"


def main():

    print("\n")
    print("********************")
    for folder in os.listdir(PATH):
        print(folder)
    print("********************")
    project = input("Choose project: ")
    print("\n")

    folders = os.listdir(PATH)
    while project:
        if project in folders:
            break
        else:
            print("Project doesn't exist.")
            project = input("Choose project: ")

    os.system(f"cd /home/simon/Linux_Storage/Projects/{project}")

if __name__ == "__main__":
    main()

and the shell script (maybe I should change something here):

function open() {
    python3 .open.py
    code .
}
Simon
  • 107
  • 9
  • What your ultimate end-goal here? The Python-script will launch it's own process when you start it, so it cannot (easily) integrate with your your VTY. When you're calling `os.system` you aren't calling the OS through your own terminal, it's calling the OS directly with it's own calls. – Hampus Larsson Jul 29 '20 at 12:46
  • If you just want to have easy access to some commands, then a [bash-alias](https://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias) might be what you're after instead. – Hampus Larsson Jul 29 '20 at 12:47
  • basically I need to change the terminal directory from the python file so that I can open VSCode directly from that directory. I am launching the python file through a custom shell command that I created. – Simon Jul 29 '20 at 12:52
  • Finally I found the solution. I looked for it for like 4 days without finding anything! Thank you very much! maybe add this as an answer so it can be useful in the future :)! – Simon Jul 29 '20 at 13:10

2 Answers2

0

Use os.chdir() instead. Running that command spawns a child with that command.

Also answered here Subprocess changing directory

Joe Tan
  • 116
  • 3
0

Use os.chdir() instead of os.system('cd /home/simon/Desktop')

#change directory
os.chdir('/home/simon/Desktop') 

#get current path
os.getcwd()

#execute script 
exec(open('path/to/yourfile.py').read())

#then change the path back to the current working directory 
Dharman
  • 30,962
  • 25
  • 85
  • 135