What's the working directory when using IDLE?
Asked Answered
A

6

39

So, I'm learning Python and would like to create a simple script to download a file from the internet and then write it to a file. However, I am using IDLE and have no idea what the working directory is in IDLE or how to change it. How can I do file system stuff in IDLE if I don't know the working directory or how to change it?

Allveta answered 4/4, 2013 at 20:30 Comment(0)
M
53

You can easily check that yourself using os.getcwd:

>>> import os
>>> os.getcwd()
'C:\\Program Files\\Python33'

That’s on my Windows machine, so it’s probably the installation directory of Python itself.

You can change that directory at runtime using os.chdir:

>>> os.chdir('C:\\Users\\poke\\Desktop\\')
>>> os.getcwd()
'C:\\Users\\poke\\Desktop'
>>> with open('someFile.txt', 'w+') as f:
        f.write('This should be at C:\\Users\\poke\\Desktop\\someFile.txt now.')

This will—not surprisingly—create the file on my desktop.

Monagan answered 4/4, 2013 at 20:33 Comment(2)
I think if I change the directory , it is unchanged again after restarting the IDLE. is it? However it's usefulBalcony
@RamisaAnjumAditi Yes, changes to current working directory are only temporary.Monagan
U
8

You can check that using os.getcwd():

In [1]: import os

In [2]: os.getcwd()
Out[2]: '/home/monty'

In [7]: os.chdir("codechef")    #change current working directory

In [8]: os.getcwd()
Out[8]: '/home/monty/codechef'

os.chdir():

In [4]: os.chdir?
Type:       builtin_function_or_method
String Form:<built-in function chdir>
Docstring:
chdir(path)

os.getcwd():

Change the current working directory to the specified path.

In [5]: os.getcwd?
Type:       builtin_function_or_method
String Form:<built-in function getcwd>
Docstring:
getcwd() -> path

Return a string representing the current working directory.
Ubiety answered 4/4, 2013 at 20:32 Comment(1)
@Allveta use os.chdir() for that.Ubiety
H
6

This will depend on OS and how IDLE is executed.

To change the (default) CWD in Windows, right click on the Short-cut Icon, go to "Properties" and change "Start In".

Histamine answered 4/4, 2013 at 20:35 Comment(1)
can you not change the default cwd from any of the toolbar options? I have looked an cant find it.Fruit
P
3

Here is an excerpt from usfca.edu

If you want to be able to import your files easily in IDLE, you need to make sure the working directory for IDLE is set to the folder with all of your code. For example, my in-class code is located at the directory /Users/sjengle/Desktop/Code, so to change the working directory of IDLE I need to run the following two commands:

import os
os.chdir("/Users/sjengle/Desktop/Code")
Ptolemaeus answered 4/4, 2013 at 20:36 Comment(0)
S
0

If you want Idle's File Open/Save/Save As menu items to interact with a particular folder, you need to set the CWD before starting Idle. So, assuming you have a folder on Windows "C:\Users<username>\Documents\python\my_project", then at a cmd prompt type cd C:\Users\<username>\Documents\python\my_project and then start Idle

Stigmatic answered 9/9, 2021 at 18:4 Comment(0)
U
0

The original question did not specify a platform. And while the accepted answer shows how to modify working directory after Idle launches, it may be useful for some (as it was for me) to modify the launch behavior directly.

I'm running Idle 3.11 on MacOS Ventura, installed using the official Python distribution installer.

Open the IDLE.app bundle: in the Python 3.11 folder in Applications, open the IDLE.app application bundle by right clicking it and selecting Show Package Contents.

In Contents>Resources you will find idlemain.py. This is the "Bootstrap script for IDLE as an application bundle." You will need sudo to edit this file. So open a Terminal (use the Finder shortcut to open Terminal at the Resources folder, if you have the shortcut installed)

or,

Open a Terminal Window and ... cd '/Applications/Python 3.11/IDLE.app/Contents/Resources' (substitute whatever version of Python you have installed).

sudo vi idlemain.py (or whatever text editor you prefer)

Search for "os.chdir" and find a line like this...

os.chdir(os.path.expanduser('~/Documents'))

and change it to whatever startup directory makes sense to you. For me it is:

os.chdir(os.path.expanduser('~/Develop/Python'))

Save the file and restart Idle. File Open now opens in the new directory by default and File Save As... dialog also opens in that new directory by default.

I don't have access to the Linux or Windows platforms so can't verify where the idlemain.py equivalent is in those distributions.

And there may be a more correct way to do this that I'm unaware of despite digging into Idle infrequently for a number of years. I hope this is helpful to others.

Note: Be aware that there are other versions of Python lurking in MacOS that are used by other frameworks and package managers. The REPLs for those python installations have their own startup code. This fix only applies to the IDLE app installed with the official MacOS Python Distribution.

Unionist answered 24/7, 2023 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.