I want to save a file without giving particular path so that it can save anywhere in the computer. I gave save_path = '/home/user/Desktop/Allinone/books'
but without this path how to save a file (it can be anywhere).
Asked
Active
Viewed 599 times
-2

Jainmiah
- 439
- 6
- 16
-
1https://stackoverflow.com/a/9536767/5562041 – E_K Jun 10 '20 at 05:21
-
2If you do not specify a path, it usually uses the *current working directory*, so, assuming you have write permissions on it, that may just be enough. Otherwise, you want to look into writing a temporary file. – norok2 Jun 10 '20 at 05:24
-
@norok2 can you write a bit of code on it. – Jainmiah Jun 10 '20 at 05:27
-
1@EsirKingsI am unable to see the relevance of that link, or indeed its use to anybody anywhere. – user207421 Jun 10 '20 at 10:11
-
*but without this path how to save a file (it can be anywhere)* - do you mean to save randomly anywhere in the system? Or what will happen if you don't give a path? Try to be more clear in your question – Tomerikoo Jun 10 '20 at 11:01
1 Answers
0
When you don't give path the file gets stored in the current working directory for most of IDE's.Current working directory means the directory you were in at time of running the program.
If you are opening a already existing file you must give the path.
Otherwise if you don't give a path to open a file in read mode, it will throw an error if the file is not present in current working directory.
if you don't give a path to open a file in write mode and file does not exists in current working directory, it will create a new file in current working directory.
Here is the code for 4th condition,
with open('loop.txt','w+') as f:
f.write("loop created")

Prathamesh
- 1,064
- 1
- 6
- 16
-
1'Current working directory' means the directory you were in when you ran the program, which isn't necessarily anything to do with where the program is located. 'System does not saves the file anywhere' is not correct. (2) is not correct if the file is in the current working directory. – user207421 Jun 10 '20 at 08:18