Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open() function python default directory

Tags:

python

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

like image 676
Terence Chow Avatar asked Jun 17 '12 17:06

Terence Chow


2 Answers

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!

like image 96
Rajesh Avatar answered Sep 23 '22 20:09

Rajesh


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.

like image 24
DS9182 Avatar answered Sep 25 '22 20:09

DS9182