14

I'm new and I have no idea where the default directory for the open() function is.

For example open('whereisthisdirectory.txt','r')

Can someone advise me? I've tried googling it (and looking on stackoverflow) and even putting a random txt file in so many folders but I still can't figure it out. Since I'm beginning, I want to learn immediately rather than type "c:/directory/whatevevr.txt" every time I want to open a file. Thanks!

Ps my python directory has been installed to C:\Python32 and I'm using 3.2

9 Answers 9

28

os.getcwd()

Shows the current working directory, that's what open uses for for relative paths. You can change it with os.chdir.

Sign up to request clarification or add additional context in comments.

Comments

5

If you working on Windows OS first type

import os

then type

os.getcwd()

and it should print the current working directory.

1 Comment

1. This should work on any recent OS. 2. I think the question is more about the concept of the current directory. Maybe you could add some sentences to explain it.
5

The answer is not python-specific. As with programs written in any other language, the default directory is whatever your operating system considers the current working directory. If you start your program from a command prompt window, the CWD will be whatever directory you were in when you ran the program. If you start it from a Windows menu or desktop icon, the CWD is usually defined alongside the program's path when creating the icon, or else falls back to some directory that Windows uses in the absence of that information.

In any case, your program can query the current working directory by calling os.getcwd().

2 Comments

The question is Python specific.
No, it is not. The question is about behavior that is common across every programming language that makes open() (or equivalent) system calls. The question would be language-specific if it was about a language that deviated from that common behavior. That is not the case here.
3

The default location is the CWD (Current Working Directory), so if you have your Python script in c:\directory and run it from there, if you call open() it will attempt to open the file specified in that location.

Comments

2

First, you must import:

import os

Then to print the current working directory:

os.getcwd()

If you want to change the current working directory:

os.chdir('your_complete_required_path')

Comments

1

create the .txt file in the directory where u have kept .py file(CWD) and run the .py file.

Comments

1

The open() function for file always creates files in the current working directory. The best way to find out your current working directory is to find three lines of small code:

import os
current_working_directory = os.getcwd()
print(current_working_directory)

Run this above code and you will get your current working directory where open() function creates a new file. Good Luck!

Comments

1

If you’re running your script through an interpreter (i.e pycharm, VSCode etc) your Python file will be saved, most likely, in my documents (at least in VSCode, in my personal experience) unless you manually save it to a directory of your choosing before you run it. Once it is saved, the interpreter will then use that as you current directory so any saves your Python script will create will also automatically go there unless you state otherwise.

Comments

0

it depends on how you run it from the terminal

like this, it is going to look in your home directory

C:\Users\name>python path\file.py

and like this, it is going to look next to your file

C:\Users\name>cd path

C:\Users\name\path>python file.py

Comments

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.