473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Please help me finding a way to implement os.path.issubpath(a, b)

Hi,
I'm trying to implement a function which returns whether a path is a
subpath of another one (e.g. /a/b/c is a subpath of /a/b).
I wrote this function which apparently seems to work fine:

import os

def issubpath(path1, path2):
"""Return True if path1 is a sub path of path2."""
if path1 == path2:
return False
x1 = path1.split(os.sep)
x2 = path2.split(os.sep)
return x1[:len(x2)] == x2

....but if I use "issubpath('C:\\dir', 'C:\\')" it returns False.
A little help would be appreciated.

Thanks in advance.
--- Giampaolo
http://code.google.com/p/pyftpdlib/

Sep 11 '08 #1
4 1812
On Sep 11, 5:40*pm, "Giampaolo Rodola'" <gne...@gmail.comwrote:
Hi,
I'm trying to implement a function which returns whether a path is a
subpath of another one (e.g. /a/b/c is a subpath of /a/b).
I wrote this function which apparently seems to work fine:

import os

def issubpath(path1, path2):
* * """Return True if path1 is a sub path of path2."""
* * if path1 == path2:
* * * * return False
* * x1 = path1.split(os.sep)
* * x2 = path2.split(os.sep)
* * return x1[:len(x2)] == x2

...but if I use "issubpath('C:\\dir', 'C:\\')" it returns False.
A little help would be appreciated.

Thanks in advance.
That's because:
>>'C:\\dir'.split('\\')
['C:', 'dir']
>>'C:\\'.split('\\')
['C:', '']

So you could write instead something like

x1 = path1.rstrip(os.sep).split(os.sep)
x2 = path2.rstrip(os.sep).split(os.sep)

in your function

HTH

--
Arnaud

Sep 11 '08 #2
Giampaolo Rodola' schrieb:
Hi,
I'm trying to implement a function which returns whether a path is a
subpath of another one (e.g. /a/b/c is a subpath of /a/b).
I wrote this function which apparently seems to work fine:

import os

def issubpath(path1, path2):
"""Return True if path1 is a sub path of path2."""
if path1 == path2:
return False
x1 = path1.split(os.sep)
x2 = path2.split(os.sep)
return x1[:len(x2)] == x2

...but if I use "issubpath('C:\\dir', 'C:\\')" it returns False.
A little help would be appreciated.
Any reason why

os.path.normpath(a).startswith(os.normpath(b)) doesn't do the trick?

Diez
Sep 11 '08 #3
Diez B. Roggisch wrote:
Any reason why

os.path.normpath(a).startswith(os.normpath(b)) doesn't do the trick?
Except for the trivial type, you mean? That depends on whether "c:\foo"
should be seen as a subpath to "c:\foobar" or not. I'd probably go for
(also untested):

def issubpath(a, b):
def fixpath(p):
return os.path.normpath(p) + os.sep
return fixpath(a).startswith(fixpath(b))

</F>

Sep 11 '08 #4
On Sep 11, 8:04*pm, Arnaud Delobelle <arno...@googlemail.comwrote:
On Sep 11, 5:40*pm, "Giampaolo Rodola'" <gne...@gmail.comwrote:
Hi,
I'm trying to implement a function which returns whether a path is a
subpath of another one (e.g. /a/b/c is a subpath of /a/b).
I wrote this function which apparently seems to work fine:
import os
def issubpath(path1, path2):
* * """Return True if path1 is a sub path of path2."""
* * if path1 == path2:
* * * * return False
* * x1 = path1.split(os.sep)
* * x2 = path2.split(os.sep)
* * return x1[:len(x2)] == x2
...but if I use "issubpath('C:\\dir', 'C:\\')" it returns False.
A little help would be appreciated.
Thanks in advance.

That's because:
>'C:\\dir'.split('\\')
['C:', 'dir']
>'C:\\'.split('\\')

['C:', '']

So you could write instead something like

* * x1 = path1.rstrip(os.sep).split(os.sep)
* * x2 = path2.rstrip(os.sep).split(os.sep)

in your function

HTH

--
Arnaud
Thanks, it seems to work just fine.
--- Giampaolo
http://code.google.com/p/pyftpdlib/
Sep 12 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Sean Ross | last post by:
Hi. Recently I made a small script to do some file transferring (among other things). I wanted to monitor the progress of the file transfer, so I needed to know the size of the files I was...
11
by: Fuzzyman | last post by:
What's the best, cross platform, way of finding out the directory a script is run from ? I've googled a bit, but can't get a clear answer. On sys.argv the docs say : argv is the script name...
0
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If...
20
by: Webdad | last post by:
Hi! I running my first year as industrial engineer (informatics) We have an assignment to do : .... create a playfield (matrix). Some places in that field are blocked, so you can't pass them....
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
8
by: inFocus | last post by:
Hello, I am new to python and wanted to write something for myself where after inputing two words it would search entire drive and when finding both names in files name would either copy or move...
3
by: DebuggerCLL | last post by:
This is an A* path finding demo I created a few months ago. I don't see any real uses for it, but it's still neat. http://clindsey.blogspot.com/2008/05/javascript-path-finding-prototype.html
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.