pathlib

From stat().st_mtime to datetime?

99封情书 提交于 2019-12-18 01:29:11
问题 What is the most idiomatic/efficient way to convert from a modification time retrieved from stat() call to a datetime object? I came up with the following (python3): from datetime import datetime, timedelta, timezone from pathlib import Path path = Path('foo') path.touch() statResult = path.stat() epoch = datetime(1970, 1, 1, tzinfo=timezone.utc) modified = epoch + timedelta(seconds=statResult.st_mtime) print('modified', modified) Seems round a bout, and a bit surprising that I have to hard

How to glob two patterns with pathlib?

懵懂的女人 提交于 2019-12-11 15:58:58
问题 I want find two types of files with two different extensions: .jl and .jsonlines . I use from pathlib import Path p1 = Path("/path/to/dir").joinpath().glob("*.jl") p2 = Path("/path/to/dir").joinpath().glob("*.jsonlines") 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? 回答1: Try this: from os.path import join from glob import glob files = [] for ext in ('*.jl', '*.jsonlines'): files.extend(glob(join(

pathlib Path resolves installed path directory of package instead of source code directory

╄→гoц情女王★ 提交于 2019-12-11 07:34:31
问题 I have packaged my project using setup.py and project folder structure looks like below. api-automation api packagename __init__.py user.py payloads a.json b.json tests conftest.py setup.cfg setup.py README.rst I have created virtual environment in below folder with name "myenv_1", /Users/basavarajlamani/Documents/environments/ and i have installed above repo in this virtual environment. I tried a lot on stackoverflow and internet but did not found answer. code of user.py file from pathlib

create new folder in python with pathlib and write files into it

﹥>﹥吖頭↗ 提交于 2019-12-06 18:54:51
问题 I'm doing something like this: import pathlib p = pathlib.Path("temp/").mkdir(parents=True, exist_ok=True) with p.open("temp."+fn, "w", encoding ="utf-8") as f: f.write(result) Error message: AttributeError: 'NoneType' object has no attribute 'open' Obviously, based on the error message, mkdir returns None . Jean-Francois Fabre suggested this correction: p = pathlib.Path("temp/") p.mkdir(parents=True, exist_ok=True) with p.open("temp."+fn, "w", encoding ="utf-8") as f: ... This triggered a

pathlib Path and py.test LocalPath

大憨熊 提交于 2019-12-05 06:22:10
I have started using pathlib.Path some time ago and I like using it. Now that I have gotten used to it, I have gotten sloppy and forget to cast arguments to str . This often happens when using tox + py.test with temporary directories based on tmpdir (which is a py._path.local.LocalPath ): from pathlib import Path import pytest def test_tmpdir(tmpdir): p = Path(tmpdir) / 'testfile.csv' Instead of inserting str() every time, I looked at solving this more generally, but could not. First I tried to make my own Path class that has an adapted _parse_args : import pytest from py._path.local import

How can I change directory with Python pathlib

隐身守侯 提交于 2019-12-03 23:08:39
What is the intended way to change directory using the Python pathlib (Documentation) functionality? Lets assume I create a Path object as follows: from pathlib import Path path = Path('/etc') Currently I just know the following, but that seems to undermine the idea of pathlib . import os os.chdir(str(path)) Based on the comments I realized that pathlib does not help changing directories and that directory changes should be avoided if possible. Since I needed to call bash scripts outside of Python from the correct directory, I opted for using a context manager for a cleaner way of changing

When using pathlib, getting error: TypeError: invalid file: PosixPath('example.txt')

别等时光非礼了梦想. 提交于 2019-12-03 14:25:24
问题 I'm using Python 3's pathlib module, like this: from pathlib import Path filename = Path(__file__).parent / "example.txt" contents = open(filename, "r").read() But I get this error on some machines: TypeError: invalid file: PosixPath('example.txt') But on my machine it works. 回答1: pathlib integrates seemlessly with open only in Python 3.6 and later. From Python 3.6's release notes: The built-in open() function has been updated to accept os.PathLike objects, as have all relevant functions in