要獲取文件路徑,可以使用Python中的os模塊的方法。以下是幾種常用的方法:
import os
file_path = 'example.txt'
absolute_path = os.path.abspath(file_path)
print(absolute_path)
import os
current_dir = os.getcwd()
file_name = 'example.txt'
absolute_path = os.path.join(current_dir, file_name)
print(absolute_path)
import os
file_path = 'path/to/example.txt'
dir_path = os.path.dirname(file_path)
print(dir_path)
這些方法可以根據需要選擇使用,根據文件路徑的不同情況來獲取文件的路徑信息。