问题标签 [pathlib]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
python - How to glob two patterns with pathlib?
I want find two types of files with two different extensions: .jl
and .jsonlines
. I use
but I want p1
and p2
as one variable not two. Should I merge p1
and p2
in first place? Are there other ways to concatinate glob's patterns?
python - 如何将字符串附加到 Python 中的路径?
以下代码:
得到以下错误:
我显然在这里做了一些阴暗的事情,但它提出了一个问题:如何访问Path
对象的子目录?
python - Pathlib 使用 Path.parents 访问路径时出错
为什么当我在 Python IDE (PyCharm) 中运行以下代码段时:
它工作正常并返回所需的结果,但是当我通过命令行运行脚本时,出现错误:
我在这里想念什么?还有一种更好/更简单的方法可以从我运行脚本的当前路径向上移动两个文件夹(在脚本内)?
python - 为什么 pathlib.Path("C:") 解析为 Windows 上的工作目录?
在 Windows 7 x64 上使用 Python 3.6,路径"C:"
似乎与空路径相同Path.resolve()
:
“空”路径是“当前工作目录” cwd()
:
单个字母被解释为文件夹名称:
一个完整的驱动器号 + 冒号 + 反斜杠按预期指向驱动器根:
但是忘记反斜杠指向当前工作目录?
我希望它将冒号(不带反斜杠)视为常规字符(它这样做是为了Path("te:st")
),或者忽略它("C"
),或者将路径视为驱动器根("C:\"
)。但相反,它似乎完全忽略了 C。
对于其他驱动器号("A:"
, "X:"
, ...),解决要么无限期挂起(不好!),要么要求我将磁盘插入驱动器(这表明它也没有完全忽略驱动器号)。
python - 如何修补 `pathlib.Path.exists()` 方法?
我想为单元测试修补对象的exists()
方法,pathlib.Path
但我无法让它工作。
我想做的是:
但它失败了:
AttributeError: 'PosixPath' object attribute 'exists' is read-only
.
有任何想法吗?
python - pathlib Path中斜杠运算符和逗号分隔符之间的区别
我读到我们可以通过在两条路径之间pathlib
使用来创建子路径/
,其中逗号也可以使用。但是,我无法确定这两种情况之间是否存在差异。在以下示例中,输出是相同的:
但是考虑到它pathlib
是面向对象的,我想在幕后可能会有一些不同的东西。/
在 Path 中使用与逗号相比有区别吗?
python - 将 pathlib 用于 S3 路径
我想构建一些功能来在 S3 和我的本地文件系统之间移动文件,但pathlib
似乎结合了重复的斜杠,破坏了我的 aws-cli 功能:
如何以这种方式操作 S3 路径?
python - 将另一个后缀添加到已经具有 pathlib 后缀的路径
我正在转换一些旧的 Python 代码以使用pathlib
而不是os.path
用于大多数与路径相关的操作,但我最终遇到了以下问题:我需要向已经有扩展名的路径添加另一个扩展名(而不是替换它)。使用os.path
,因为我们只是在操作字符串,解决方案是添加带有字符串操作的扩展:
它不起作用,pathlib.Path
因为它不允许连接任意字符。我能找到的最接近的是以下内容:
它看起来像是一种解决方法,因为它最终仍然使用字符串添加。它有一个新的陷阱,因为我一开始忘记处理已经有几个扩展并且你想添加一个新的情况,导致下面的代码恢复旧的行为:
现在感觉既不简洁也不干净,因为它使用越来越多的字符串操作来实现旧的行为,而不是纯粹的路径操作。存在的事实Path.suffixes
意味着库的开发人员考虑了文件可以具有多个扩展名的情况,但我找不到简单地向路径添加新扩展名的方法。有没有更惯用的方式来实现相同的行为?
编辑:实际上path.with_suffix(path.suffix + '.res')
足以处理已经有多个文件扩展名的情况,即使这对我来说并不是很明显。
python - Unexpected results from Path.read_text (of pathlib) when reading utf-8 encoded file
Today I learned that for open(filename).read()
we cannot expect that the resources bound to the hidden file object are given back immediately, although I observed this on my system. (See the accepted answer of the question Does reading an entire file leave the file handle open?).
The second answer made me resist to roll my own helper function, it told me that pathlib
already offers exactly this function.
But actually, this seems not to be the case. With the following script (test.py
), I get different results:
The output to the Windows console (python test.py
) contains Ã",Ã-,Ão,ä,ö,ü, and ÃY.
in the first block, when I redirect the output into a file, I get the second block wrong (In Notepad++ it's displayed xC4,xD6,xDC,xE4,xF6,FC, and xDF
in White on Black) if the file is treated as utf-8.
Is there anything I overlooked?
I tried to examine the 3.6.3 code, but found no bug so far ...
Edit
The following version reinforces my feeling that it's a bug in pathlib
or in one of the underlying libraries/functions. Maybe it's only a Windows issue, where the default encoding is mostly different from utf-8
. Now it's sufficient to run the test in a console window.
It produces the following output:
python-3.x - Python:将文件上移一个目录
下面是一个小测试例程,它获取文件的路径并将文件向上移动一个目录。我正在使用 os 和 shutil 模块,是否有一个模块可以执行此任务?有没有更 Pythonic 的方式来实现这个功能?
下面的代码在 Windows 上运行,但最好的跨平台解决方案将不胜感激。