Jump to content

Changing the directory in the terminal of the VS Code

Hello,

 

When I am trying to create a new file from inside python using "open", the file is created in the location from where the terminal is run (in my case /User). Now I can change the location of this new file in 2 ways,

1. By including the desired path in my script.

2. Changing the path of the terminal (using "cd" command).

 

But every time I have to do this manually.
Say I want to create this new file in the location of the existing file i.e the file which is creating the new file. And this existing file changes its location every now and then. Then I have to manually perform either the step 1 or 2 every single time. 

 

Now Idle creates this new file in the same location as the existing one.

 

I was wondering if I could do the same using VS Code.

 

Thanks.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, tobeornottobealttfan said:

I was wondering if I could do the same using VS Code.

Have your Python-code write the file in the correct location, instead of relying on the programming-environment.

 

See e.g. the following:

def saveConfig():
	try:
    	# __file__ contains the entire path to your script, so use it to construct the path for your file
		f = open(__file__.rsplit("/", 1)[0] + "/config.txt", "w")
	except IOError:
		print("Error: cannot open config.txt for writing, can't save settings!", file=sys.stderr)
		return -1
	else:
		f.write("Put something in the file")
		f.close()
		return 0

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, WereCatf said:

Have your Python-code write the file in the correct location, instead of relying on the programming-environment.

 

See e.g. the following:


def saveConfig():
	try:
    	# __file__ contains the entire path to your script, so use it to construct the path for your file
		f = open(__file__.rsplit("/", 1)[0] + "/config.txt", "w")
	except IOError:
		print("Error: cannot open config.txt for writing, can't save settings!", file=sys.stderr)
		return -1
	else:
		f.write("Put something in the file")
		f.close()
		return 0

 

I didn't understand quite a few things. 
Do I have to call this every single time I want to create a new file or do I have to write it every single time I want to create a new file from inside a file? 

Link to comment
Share on other sites

Link to post
Share on other sites

If you open a folder with vscode then the path in terminal should be the same.

For example: create a folder on your desktop let's say myProject, then you can go to File->Open Folder in vscode, and the terminal shoud open up in here: /user/desktop/myProject

 

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×