Reputation: 615
I have a problem with python. Looks like python has as default folder my Dropbox folder. Each time that I want to save some file without specifies the path, pythons save it automatically in the Dropbox folder.
>>> saveFile = save('Im a file', 'w')
>>> saveFile.write('Hello World')
>>> saveFile.close()
The file Im a file
is saved in the Dropbox folder instead the Home folder. How can I change this? This also occurs when I want to load some file stored in Home folder to python, I must specify the path to Home, but not when the file is stored in the Dropbox folder.
My OS is Deepin (a distro based on Ubuntu)
Thanks to all.
Upvotes: 1
Views: 10927
Reputation: 605
Usually when you create a file without specifying the path in python, e.g. save('myfile')
, it will be created in the directory where you ran the code from.
To stop this happening, you can specify the whole path
save('path/to/file/myfile')
or if you are running in terminal move to the correct directory and run your script from there
python path/to/script/myscript.py
or even move/copy your code into the directory where you want the file.
The first option is probably best in the general case, but it will depend on exactly when you typically use your script.
Upvotes: 0
Reputation: 1779
Files are saved and opened relative to current working directory. By default, that's the location of the program file. You have to change it in the program if you like, as follows,
>>> import os;
>>> os.getcwd();
'/home/foo'
>>> os.chdir('/tmp');
>>> os.getcwd();
'/tmp'
But it's not recommended to hardcode these things.
Upvotes: 1
Reputation: 3457
You have to change directory. Your default path should be where IDLE is installed. (I'm assuming you're using IDLE).
import os
os.chdir(yourpathgoeshere)
Alternatively you can try invoking IDLE from the directory you want to be your current working directory.
cd path/that/you/want/to/use
/usr/bin/IDLE (Wherever IDLE is installed, you should be able to do `which IDLE` to see the dir)
It looks like you're doing cd ~/Dropbox, then running IDLE/Python from there. That will set your default directory to ~/Dropbox.
Upvotes: 0