擅长:python、mysql、java
<p>您可以在Python 3.0+中使用<strong>pathlib</strong>包</p>
<p>这将获取包含在<strong>SampleProject</strong>文件夹中的任何文件的路径
跨不同的平台。
</p>
<pre><code>from pathlib import Path
def get_file(path):
"""
returns the absolute path of a file
:var
str path
the file path within the working dir
:returns
PureWindowsPath or PurePosixPath object
type depends on the operating system in use
"""
def get_project_root() -> Path:
"""Returns project root folder."""
return Path(__file__).parent.parent
return get_project_root().joinpath(path)
</code></pre>
<p>然后用<strong>文件路径</strong>作为参数调用函数:
</p>
<pre><code>filePath = get_file('com/python/example/source/utils/configManager.py')
</code></pre>
<p>然后是通常的程序:
</p>
<pre><code>while open(filePath) as f:
<DO YOUR THING>
</code></pre>