我正在构建一个简单的助手脚本,用于将代码库中的两个模板文件复制到当前目录。但是,我没有存储模板的目录的绝对路径。我有一个相对路径从脚本,但当我调用脚本,它把它作为一个相对于当前工作目录的路径。是否有一种方法来指定这个相对url是来自脚本的位置?
当前回答
以下是我的总结:
首先,定义名为relpath的工具函数,它将当前文件的相对路径转换为cwd的相对路径
import os
relpath = lambda p: os.path.normpath(os.path.join(os.path.dirname(__file__), p))
然后我们使用它来包装相对于当前文件的路径
path1 = relpath('../src/main.py')
你也可以调用sys.path.append()来导入相对于当前文件位置的文件
sys.path.append(relpath('..')) # so that you can import from upper dir
完整的示例代码:https://gist.github.com/luochen1990/9b1ffa30f5c4a721dab5991e040e3eb1
其他回答
我认为要在所有系统中使用“ntpath”而不是“os.path”。如今,它在Windows、Linux和Mac OSX上都能很好地工作。
import ntpath
import os
dirname = ntpath.dirname(__file__)
filename = os.path.join(dirname, 'relative/path/to/file/you/want')
现在是2018年,Python很久以前就已经进化到__future__了。因此,如何使用Python 3.4附带的惊人的pathlib来完成任务,而不是在os, os中苦苦挣扎。路径,glob, shutil等。
这里有3条路径(可能重复):
Mod_path:简单帮助脚本的路径 Src_path:包含两个等待复制的模板文件。 Cwd:当前目录,这些模板文件的目的地。
问题是:我们没有src_path的完整路径,只知道它到mod_path的相对路径。
现在让我们用惊人的pathlib来解决这个问题:
# Hope you don't be imprisoned by legacy Python code :)
from pathlib import Path
# `cwd`: current directory is straightforward
cwd = Path.cwd()
# `mod_path`: According to the accepted answer and combine with future power
# if we are in the `helper_script.py`
mod_path = Path(__file__).parent
# OR if we are `import helper_script`
mod_path = Path(helper_script.__file__).parent
# `src_path`: with the future power, it's just so straightforward
relative_path_1 = 'same/parent/with/helper/script/'
relative_path_2 = '../../or/any/level/up/'
src_path_1 = (mod_path / relative_path_1).resolve()
src_path_2 = (mod_path / relative_path_2).resolve()
在未来,就这么简单。
此外,我们可以使用pathlib选择、检查和复制/移动这些模板文件:
if src_path != cwd:
# When we have different types of files in the `src_path`
for template_path in src_path.glob('*.ini'):
fname = template_path.name
target = cwd / fname
if not target.exists():
# This is the COPY action
with target.open(mode='wb') as fd:
fd.write(template_path.read_bytes())
# If we want MOVE action, we could use:
# template_path.replace(target)
看到sys.path 在程序启动时初始化后,该列表的第一项路径[0]是包含用于调用Python解释器的脚本的目录。
使用此路径作为根文件夹,从中应用相对路径
>>> import sys
>>> import os.path
>>> sys.path[0]
'C:\\Python25\\Lib\\idlelib'
>>> os.path.relpath(sys.path[0], "path_to_libs") # if you have python 2.6
>>> os.path.join(sys.path[0], "path_to_libs")
'C:\\Python25\\Lib\\idlelib\\path_to_libs'
从C:\Users\xyz\myFolder到C:\Users\xyz\testdata:
import os
working_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
# C:\Users\xyz\myFolder
print(working_dir)
updated_working_dir = os.path.join(os.path.realpath(working_dir + '/../'), 'testdata')
# C:\Users\xyz\testdata
print(updated_working_dir)
输出
C:\Users\xyz\myFolder
C:\Users\xyz\testdata
Hi首先你需要理解os。path。abspath(path)和os。path。relpath(path)
简而言之,os.path.abspath(path)将相对路径转换为绝对路径。如果提供的路径本身是一个绝对路径,那么函数返回相同的路径。
类似地,os.path.relpath(path)将绝对路径转换为相对路径。如果提供的路径本身是相对路径,则函数返回相同的路径。
下面的例子可以让你正确理解上面的概念:
假设我有一个文件input_file_list.txt,其中包含要由我的python脚本处理的输入文件列表。
D: \ \ input1.dic浓缩
D: \ \ input2.dic浓缩
D: \ Copyioconc input_file_list。txt
如果你看到上面的文件夹结构,input_file_list.txt在Copyofconc文件夹中,python脚本要处理的文件在conc文件夹中
但是input_file_list.txt文件的内容如下所示:
. . \ \ input1.dic浓缩
. . \ \ input2.dic浓缩
我的python脚本在D: drive中。
input_file_list.txt文件中提供的相对路径是相对于input_file_list.txt文件的路径。
因此,当python脚本执行当前工作目录时(使用os.getcwd()获取路径)
由于我的相对路径是相对于input_file_list.txt,即“D:\Copyofconc”,我必须将当前工作目录更改为“D:\Copyofconc”。
所以我必须使用os.chdir('D:\Copyofconc'),所以当前的工作目录应该是“D:\Copyofconc”。
现在获取文件input1。Dic和input2。Dic,我将读取“..\conc\input1”这行。然后使用命令
Input1_path = os.path.abspath('..\conc\input1.dic')(将相对路径更改为绝对路径。这里作为当前的工作目录是“D:\Copyofconc”,文件“.\conc\input1.”相对于"D:\Copyofconc")
所以input1_path应该是“D:\conc\input1.dic”
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录