pathlib

How to get absolute path of a pathlib.Path object?

血红的双手。 提交于 2020-07-17 03:05:46
问题 Making a path object with pathlib module like: p = pathlib.Path('file.txt') The p object will point to some file in the filesystem, since I can do for example p.read_text() . How can I get the absolute path of the p object in a string? Appears that I can use for example os.path.abspath(p) to get the absolute path, but it awkward to use an os.path method, since I assume that pathlib should be a replacement for os.path . 回答1: You're looking for the method .absolute , if my understanding is

How to test if object is a pathlib path?

你说的曾经没有我的故事 提交于 2020-06-15 12:18:13
问题 I want to test if obj is a pathlib path and realized that the condition type(obj) is pathlib.PosixPath will be False for a path generated on a Windows machine. Thus the question, is there a way to test if an object is a pathlib path (any of the possible, Path , PosixPath , WindowsPath , or the Pure... -analogs) without checking for all 6 version explicitly? 回答1: Yes, using isinstance() . Some sample code: # Python 3.4+ import pathlib path = pathlib.Path("foo/test.txt") # path = pathlib

How to test if object is a pathlib path?

柔情痞子 提交于 2020-06-15 12:17:53
问题 I want to test if obj is a pathlib path and realized that the condition type(obj) is pathlib.PosixPath will be False for a path generated on a Windows machine. Thus the question, is there a way to test if an object is a pathlib path (any of the possible, Path , PosixPath , WindowsPath , or the Pure... -analogs) without checking for all 6 version explicitly? 回答1: Yes, using isinstance() . Some sample code: # Python 3.4+ import pathlib path = pathlib.Path("foo/test.txt") # path = pathlib

How do I append a string to a Path in Python?

放肆的年华 提交于 2020-05-12 11:42:09
问题 The following code: from pathlib import Path Desktop = Path('Desktop') SubDeskTop = Desktop + "/subdir" gets the following error: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-eb31bbeb869b> in <module>() 1 from pathlib import Path 2 Desktop = Path('Desktop') ----> 3 SubDeskTop = Desktop+"/subdir" TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str' I'm clearly doing something shady here,

Adding another suffix to a path that already has a suffix with pathlib

一世执手 提交于 2020-04-10 06:50:13
问题 I was converting some old Python code to use pathlib instead of os.path for most path-related operations, but I ended up with the following problem: I needed to add another extension to a path that already had an extension (not replace it). With os.path , since we are merely manipulating strings, the solution was to add the extension with string operations: newpath = path + '.res' It doesn't work with pathlib.Path because it doesn't allow concatenation of arbitrary characters. The closest I

Adding another suffix to a path that already has a suffix with pathlib

五迷三道 提交于 2020-04-10 06:49:10
问题 I was converting some old Python code to use pathlib instead of os.path for most path-related operations, but I ended up with the following problem: I needed to add another extension to a path that already had an extension (not replace it). With os.path , since we are merely manipulating strings, the solution was to add the extension with string operations: newpath = path + '.res' It doesn't work with pathlib.Path because it doesn't allow concatenation of arbitrary characters. The closest I

Recursively iterate through all subdirectories using pathlib

若如初见. 提交于 2020-01-10 03:28:07
问题 How can I use pathlib to recursively iterate over all subdirectories of a given directory? p = Path('docs') for child in p.iterdir(): child only seems to iterate over the immediate children of a given directory. I know this is possible with os.walk() or glob , but I want to use pathlib because I like working with the path objects. 回答1: You can use the glob method of a Path object: p = Path('docs') for i in p.glob('**/*'): print(i.name) 回答2: Use Path.rglob (substitutes the leading ** in Path()

Getting OS Error when passing string to pathlib.Path in windows

拜拜、爱过 提交于 2019-12-24 07:29:31
问题 How to pass string to pathlib.Path in Python3. I am dynamically passing normal windows path in Path(). But it is throwing error. the snippet is as below: src = "C:\Documents\Newsletters\Summer2018.pdf" rsrc = r"C:\Documents\Newsletters\Summer2018.pdf" s = pathlib.Path(src) rs = pathlib.Path(rsrc) print(s.exists()) # throws error print(rs.exists()) # returns True I want to pass normal string to Path, instead off raw string. Is there anyway to pass normal string to Path and check for its

Getting OS Error when passing string to pathlib.Path in windows

筅森魡賤 提交于 2019-12-24 07:29:06
问题 How to pass string to pathlib.Path in Python3. I am dynamically passing normal windows path in Path(). But it is throwing error. the snippet is as below: src = "C:\Documents\Newsletters\Summer2018.pdf" rsrc = r"C:\Documents\Newsletters\Summer2018.pdf" s = pathlib.Path(src) rs = pathlib.Path(rsrc) print(s.exists()) # throws error print(rs.exists()) # returns True I want to pass normal string to Path, instead off raw string. Is there anyway to pass normal string to Path and check for its

How can I change directory with Python pathlib

醉酒当歌 提交于 2019-12-21 07:01:02
问题 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)) 回答1: 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