Closed alipqb closed 4 years ago
Hey @alipqb,
is it possible that the folder /home/user/my_db/
does not exist on your computer? If it does, TinyDB should create the file you specified.
Hi, thanks for your answer, but as I mentioned above, I did it but it didn't work.
The path above is hardcoded but in my code, I have used a dynamic path using os.path
module and expanduser
function that shows my path is not wrong.
Does it work to simply open the path you use like this?
open(your_path, 'r+')
If this works, TinyDB should be able to create the file as well
It doesn't work. I think I should first make the specified path and file and then use the db = Tinydb('/home/user/my_db/db.json')
command. It seems Tinydb can't make a directory by itself for saving data.
By default, TinyDB doesn't attempt to create directories, that do not exist. If the directory exists, it will create a new file if the specified file does not exist. This is standard behavior for Python's open
as well.
By the way, if you use the JSONStorage
(which you most likely do), you can pass create_dirs=True
to create directories in the path you specify, if they are missing:
db = TinyDB('your/path/to/db.json', create_dirs=True)
Thanks for your help. my problem solved. But I have a question. Why haven't you made it the default option?
Because in most cases, trying to create file in a non-existing directory is an oversight, maybe being the result of a typo. In my view, it would be bad if I mistyped the folder name and TinyDB just started creating folders all over my file system. Thus, this behavior is opt-in.
Thanks for your answer.
Hi, guys I have followed the instructions in the documentation like below and tried to make a Tinydb instance:
db = Tinydb('/home/user/my_db/db.json')
but it displays: FileNotFoundError: [Errno 2] No such file or directory: '/home/user/my_db/db.json' Doesn't Tinydb create a json file when there is no such a file? The problem here is that it doesn't let create json file in a directory, butdb = Tinydb('db.json)'
doesn't have such a problem.