[英]How to set path to prev directory Python?
I use this function to get current path to directory where is placed Python script: 我使用此函数来获取放置Python脚本的目录的当前路径:
def get_script_path(self):
return os.path.dirname(os.path.realpath(__file__))
I want to set up path to one directory upper from current catalog to catalog /advs
: 我想设置从当前目录到目录
/advs
上一个目录的路径:
I tried: 我试过了:
filename = 'file.txt'
filepath = os.path.join(self.get_script_path(), '../advs', filename)
Try using below it will give the path of previous directory in which running script is present 尝试在下面使用它将提供存在运行脚本的上一个目录的路径
import os
path_to_prev_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
filename = 'file.txt'
filepath = os.path.join(path_to_prev_dir, 'advs', filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.