0

I'm trying to remove a directory and I guess i'm having an issue with defining the path for the shutil.rmtree(). Does this function take the relative path or the absolute. I use win10 and am having problem understanding how the paths can and should be defined in python, because it seems that for every function the path needs to be defined in a different way.

import shutil
dir_to_remove = "C:\\Users\name_of_user\\dir_to remove"
shutil.rmtree(dir_to_remove)

Result is:

Traceback (most recent call last):

  File "<ipython-input-257-9cc5f9fe51d2>", line 1, in <module>
    shutil.rmtree(dir_to_remove)

TypeError: 'str' object is not callable

Any help or guidance is appreciated.

adl
  • 1,390
  • 16
  • 36
  • I am able to remove my dir using below code dir_to_remove = "C:\\Users\\abc\\Downloads\\test" shutil.rmtree(dir_to_remove) Are you missing "\" could be like this dir_to_remove = "C:\\Users\\name_of_user\\dir_to remove" – SUN Feb 19 '20 at 14:51
  • 1
    The error message looks like you somehow managed to overwrite the `rmtree` function. As such, this seems unreproducible. What is the output of `print(type(shutil.rmtree))` and `prit(shutil.rmtree)`? – tripleee Feb 19 '20 at 15:10
  • If you can reproduce this with the same traceback without the backslash error, please [edit] so we can reopen. We'd like to see a [mre]. – tripleee Feb 19 '20 at 15:22
  • the function has been overwritten. I didn't know this is possible in Python, to overwrite a function from a package. This solved my issue. – adl Feb 19 '20 at 15:50

1 Answers1

1

You are missing the second backslash after C:\\Users.

dir_to_remove = "C:\\Users\\name_of_user\\dir_to remove"
shutil.rmtree(dir_to_remove)
tripleee
  • 175,061
  • 34
  • 275
  • 318
SUN
  • 181
  • 5
  • @tripleee, I am assuming it will have an import statement, I just provided the pseudo-code. There are missing "\" in the original statement.. – SUN Feb 19 '20 at 15:11
  • Good catch, but that would not explain the mystery traceback. – tripleee Feb 19 '20 at 15:21