0

I have a bit of a tricky one to solve. I have the need to extract a specific portion of a file path. I've extracted a zip file under a temp directory have have the full path to the file. Essentially what I would like is to get the difference between the full file path and the temp path. Let me give an example below:

Fullpath = c:\\users\\test\\appdata\\local\\temp\\tempDir\\common\\test.txt

TempPath = c:\\users\\test\\appdata\\local\\temp\\tempDir\\

So my expected results would be to have the following:

results = \\common\\test.txt

Just looking for an easy, Pythonic way to accomplish this.

Bach
  • 6,145
  • 7
  • 36
  • 61
user2643864
  • 641
  • 3
  • 11
  • 24
  • Keep in mind, this subdirectory path under the temporary directory can change. It could be 1 or many levels deep to the file. – user2643864 Apr 10 '14 at 19:32
  • A robust solution would resolve different absolute paths resolving to the same working directory. – dawg Apr 10 '14 at 19:32

3 Answers3

3

You can use os.path.relpath:

os.path.relpath(Fullpath, TempPath)

Or you can use split:

Fullpath.split(TempPath)[1]

Or you can use commonprefix with replace as:

Fullpath.replace(os.path.commonprefix([Fullpath, TempPath]),'')

Output:

common\test.txt
venpa
  • 4,268
  • 21
  • 23
  • Thanks for the reply. I like the usage of 'os.path.relpath(Fullpath, TempPath)'. I guess the remaining question I have is how would one strip the file name off and leave the directory. so for example, the results would be common\ instead of common\test.txt – user2643864 Apr 10 '14 at 20:14
0

A less than fully robust way is to use os.path.commonprefix:

import os

Fullpath = 'c:\\users\\test\\appdata\\local\\temp\\tempDir\\common\\test.txt'
TempPath = 'c:\\users\\test\\appdata\\local\\temp\\tempDir\\'

print os.path.commonprefix([Fullpath, TempPath])
# c:\users\test\appdata\local\temp\tempDir\

Be aware thought that the function does not know anything about paths; it is just a character by character deal.

Then use str.partition to get the part you are interested in:

>>> print Fullpath.partition(os.path.commonprefix([Fullpath, TempPath]))
('', 'c:\\users\\test\\appdata\\local\\temp\\tempDir\\', 'common\\test.txt')

If you have a situation like so:

Fullpath = 'c:\\users\\test\\appdata\\local\\temp\\tempDir\\common\\test.txt'
TempPath = 'c:\\users\\test\\appdata\\local\\temp\\tempDir\\co'  

It is better to wrap the common prefix with os.path.dirname

>>> os.path.dirname(os.path.commonprefix([Fullpath, TempPath]))
c:\users\test\appdata\local\temp\tempDir\

But that still does not fix a situation like this:

Fullpath = 'c:\\users\\test\\..\\test\\appdata\\local\\temp\\tempDir\\common\\test.txt'

Where you need to resolve full absolute path names before parsing.

dawg
  • 98,345
  • 23
  • 131
  • 206
0
results = '\\' + Fullpath.replace(TempPath, '')

Or if you want to be sure to remove the beginning of the string:

import re
results = '\\' + re.sub('^%s' % TempPath, '', Fullpath)
MickaëlG
  • 1,377
  • 1
  • 11
  • 17