0

Let's say I have this path D:\something\something1\from_here_I_now\stuff\stuff2.

So, I know that \from_here_I_now\stuff\stuff2 is a permanent path, but the beginning is different, like I know that D:\something\something1\ may be different for someone else. How can I find the D:\something\something1\ knowing only \from_here_I_now\stuff\stuff2?

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

Try something like this:

import os

filestr = '\from_here_I_now\stuff\stuff2'
fullstr = os.path.abspath(filestr)

print(fullstr)
>>> 'D:\something\something1\from_here_I_now\stuff\stuff2'

print(fullstr[:len(filestr)])
>>> 'D:\something\something1'
Jan Willem
  • 820
  • 1
  • 6
  • 24
  • I try, but gives me C:\something\something1\from_here_I_now\stuff\stuff2 What I mean is that it gives a not real path I have nothing on C related to it – Dumitru Untilă Jan 19 '21 at 21:58
  • You can try switching disks with `os.chdir('D:')`. To delete your folder you can try `os.rmdir('path')` for empty folders and `shutil.rmtree('path')` for non-empty folders. See a more detailed example [here](https://linuxize.com/post/python-delete-files-and-directories/) – Jan Willem Jan 21 '21 at 09:31