28668

How to test if object is a pathlib path?

<h3>Question</h3>

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?


<h3>Answer1:</h3>

Yes, using isinstance(). Some sample code:

<pre class="lang-py prettyprint-override"># Python 3.4+ import pathlib path = pathlib.Path("foo/test.txt") # path = pathlib.PureWindowsPath(r'C:\foo\file.txt') # checks if the variable is any instance of pathlib if isinstance(path, pathlib.PurePath): print("It's pathlib!") # No PurePath if isinstance(path, pathlib.Path): print("No Pure path found here") if isinstance(path, pathlib.WindowsPath): print("We're on Windows") elif isinstance(path, pathlib.PosixPath): print("We're on Linux / Mac") # PurePath else: print("We're a Pure path")

Why does isinstance(path, pathlib.PurePath) work for all types? Take a look at this diagram:

We see that PurePath is at the top, that means everything else is a subclass of it. Therefore, we only have to check this one. Same reasoning for Path to check non-pure Paths.

<em>Bonus</em>: You can use a tuple in isinstance(path, (pathlib.WindowsPath, pathlib.PosixPath)) to check 2 types at once.

来源:https://stackoverflow.com/questions/58647584/how-to-test-if-object-is-a-pathlib-path

Recommend

  • How to test if object is a pathlib path?
  • How to split string from meshed content and add html element
  • How to make Expiring/Signed video embed urls
  • Access IPV6 with IPV4 [closed]
  • how do I array from a GET request function in viewdidload function in swift
  • Randomly iterate through sequence of integers, 1 to n [duplicate]
  • How to list all Call logs from Log list including Sms and email logs also?
  • CLIENT STACK TRACE in Hibernate using c3p0
  • Given a dom element, how could I flatten it in jquery and/or JavaScript?
  • I am trying to create an app in android to insert data into sql server through a web service.
  • Image insertion from SQL info
  • invoke-webrequest to get complete web page with images
  • Print a Form at higher dpi than screen resolution
  • search bar getting disappeared in ios UIsearchcontroller
  • Texture streaming in DirectX11, Immutable vs Dynamic
  • Why does packing not work across sibling unions or structs
  • pass sessionid through jquery ajax call to php
  • Need reference code for SMO in C# SQL Server 2008
  • “RepeatForUnit” item missing in Calendar entry?
  • AngularJS Dynamic Directives inside ng-repeat
  • PySpark: Get first Non-null value of each column in dataframe
  • what is “Other” category in CosmosDB monitoring graph?
  • Adding custom message on Thank You page by shipping method
  • Preloading webView doesn't work - trying to reduce loading time
  • Access to database zend framework
  • Find corners of a rotated rectangle
  • Bison does not appear to recognize C string literals appropriately
  • How to add html image in to velocity template file to send email?
  • C# code can't “see” the methods in my C++ dll
  • Create an Office365 mailbox from within C# Web API method
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • getting the class name of an HTML tag using XPATH
  • How to create subsets of a single set of elements with XSLT?
  • how to run a different select statement based on condition in Hive SQL
  • Create an average of multiple excel chart without the data source
  • Conflicting declaration using constexpr and auto in C++11
  • I am consuming a WCF service that requires headers from a .NET 2 website. How can I programmatically
  • Using Service Component Runtime
  • How to use FirstOrDefault inside Include
  • Time Complexity of Fibonacci Algorithm [duplicate]