python 如何查看路径,Python中查看路径的方法
原创查看文件路径是Python编程中常见的操作,通常用于读取或写入文件,在Python中,可以使用多种方法查看文件路径。
1、使用os模块:
import os 获取当前工作目录 current_directory = os.getcwd() print("当前工作目录是:", current_directory) 获取文件路径 file_path = os.path.join(current_directory, "filename.py") print("文件路径是:", file_path)
2、使用sys模块:
import sys 获取当前工作目录 current_directory = sys.path[0] print("当前工作目录是:", current_directory) 获取文件路径 file_path = sys.path[0] + "/filename.py" print("文件路径是:", file_path)
3、使用os.path模块:
import os.path 获取文件路径 file_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "filename.py") print("文件路径是:", file_path)
这些方法可以帮助你在Python中查看文件路径,你可以根据自己的需求选择适合的方法。