Create a directory test on your desktop and create test.py and test.txt in the directory. test.txt is described as "test completed". Create directory text2 in the directory and create test2.txt. test2.txt describes "Test 2 completed".
test
├test.py
├test.txt (describe "test completed")
└test2
└test2.txt (describe "Test 2 completed")
(2)pwd Enter the current directory and pwd in the terminal as follows
/Users/*******/desktop/test
Get the absolute path of the current directory with os.getcwd () and get the relative path from within the current directory with \ __ file__.
import os
print(os.getcwd())
print(__file__)
The execution result is as follows.
/Users/*******/Desktop/test
file_test.py
(5)os.path.abspath() You can get the relative path from the current directory with \ __ file__, but you can convert it to an absolute path with os.path.abspath (). The directory can also be obtained with an absolute path.
import os
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))
The execution result is as follows.
/Users/*******/Desktop/test/file_test.py
/Users/*******/Desktop/test
(6)os.path.join os.path.join can combine two character strings given in the argument into one path. First, read test.txt in the directory test.
with open('test.txt',encoding='utf-8') as f:
print(f.read())
Then, it becomes as follows.
Test completed
Next, read test2.txt in the directory test2.
with open('test2/test2.txt',encoding='utf-8') as f:
print(f.read())
Then, it becomes as follows.
Test 2 completed
Next, use os.path.join to read test2 by combining the absolute path to the current directory and the relative path to test2.txt.
import os
target = os.path.join(os.path.dirname(os.path.abspath(__file__)),'test2/test2.txt')
with open(target,encoding='utf-8') as f:
print(f.read())
Then, it becomes as follows.
Test 2 completed
(7)chdir chdir Used as [destination path]. Used when moving the current directory on the terminal. First, create a destination path.
import os
target2 = os.path.join(os.path.dirname(os.path.abspath(__file__)),'test2')
Move the current directory via the set destination path.
os.chdir(target2)
Just in case, check the current directory and check if it has moved.
print(os.getcwd())
From test, I was able to confirm that the current directory could be moved to test2.
/Users/*********/Desktop/test/test2
When I read test2.txt directly (
with open('test2.txt',encoding='utf-8')as f:
print(f.read())
It is output properly as follows. (If the current directory is test, with open ('test2.txt', encoding ='utf-8') is replaced with with open ('test2.test2.txt', encoding ='utf-8') There is a need to.)
Test 2 completed
.py:test.py
import os
print(os.getcwd())
print(__file__)
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))
print('------------------------------------')
with open('test.txt',encoding='utf-8') as f:
print(f.read())
with open('test2/test2.txt',encoding='utf-8') as f:
print(f.read())
target = os.path.join(os.path.dirname(os.path.abspath(__file__)),'test2/test2.txt')
with open(target,encoding='utf-8') as f:
print(f.read())
print('------------------------------------')
target2 = os.path.join(os.path.dirname(os.path.abspath(__file__)),'test2')
os.chdir(target2)
print(os.getcwd())
with open('test2.txt',encoding='utf-8')as f:
print(f.read())
Get the location (path) of the file running in Python file
Recommended Posts