python获得文件路径的有哪些方法
要获得文件路径,可使用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)
这些方法可以根据需要选择使用,根据文件路径的区分情况来获得文件的路径信息。
TOP