How to get relative resource from Python resource roots
已回答
According to https://www.jetbrains.com/help/pycharm/content-root.html, PyCharm resource roots are defined as follows:
Resource roots
These roots are intended for resource files in your application (images, Style Sheets, etc.) By assigning a folder to this category, you tell PyCharm that files in it and in its subfolders can be referenced relative to this folder instead of specifying full paths to them.
So, if I wanted to read from a file in a resource rate (e.g. file.txt), how would I do so via a relative path?
Ihave tried open("resourceRoot/file.txt"), open("/resourceRoot/file.txt"), open("file.txt"), and open("/file.txt")
请先登录再写评论。
Hi,
You can just right click on the file and select Copy Relative Path.
But if your file is located, for example, on C:\folder\file.txt, then it will just give you "file.txt" and PyCharm will not find it. In such case you need to create a subdirectory or use Copy Path.
I see. That makes sense.