1. Absolute path
To read the relative path of the file shown in the figure:
Method 1: Manually read directly according to the file path
D:\PycharmProjects\pythonProject\torch_learning\test_path.py
Method 2: (path) fetch
path = "test_path.py" # path is the name of the file print((papathth)) # Use methods to get absolute paths # The result of the code is as follows:D:\PycharmProjects\pythonProject\torch_learning\read_data.py
2. Absolute path
For example, I want to access the file "" on the right from the file "test_path" on the left, using a relative path:
1. Get the absolute path to the current file:D:\PycharmProjects\pythonProject\torch_learning\test_path.py
2. Get the absolute path to the access file: D:\PycharmProjects\pythonProject\torch_learning\dataset\train\ants\
3. As you can see, if you want to go from "test_path.py" to "", you have to go through the "torch_learning" folder, which is equivalent to treating it as a transition folder.
4. The relative path is: ". /dataset/train/ants/"
3. Distinguish between the relative path of a program running in a window in pycharm and the path of a program running on the console.
The relative path of the program running in the window is specific to the file running at that time
And the relative path to the console to run the program is determined by where the virtual environment you created is currently located.
For example, if I create a virtual environment with the name "pythonProject1", the relative path to the Python console starts there.
P.S. Methods for importing files or modules with relative or absolute paths in Python
When debugging the code, the program has been prompted by the absence of the module, has been very puzzled, because I have been importing files with absolute path to import. It is reasonable to say that there will not be a module can not be found.
Finally a careful analysis of the entire directory structure of the code revealed the problem.
Here's a description of the day in order
Let's look at the imported code first:
existdemo——The following two import operations are performed in the file # First place from import GCForest from .config_utils import load_json # Second place config = load_json("demo_mnist") # Inside is passed the location of the file,This function reads the contents of the configuration file
Then look at the structure of the catalog
In this example, demo_mnist,py is the main file. The first thing it will import is a file in a directory that is a subdirectory of the parent directory of its current directory.
So importing this file as an absolute path would befrom import GCForest
Importing this file as a relative path isfrom .. import GCForest
The second import is another file in its unified directory, so you can directly import the
So the modified import code is:config = load_json("demo_mnist")
summarize
to this article on Python pycharm read file relative path and absolute path to this article, more related pycharm read file relative path absolute path content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!