10

So I am trying to make my very first python program to automate a task that I have. The first snippet of code is from a python script that makes a new folder at a pre-specified destination and then moves files from their original location to the new folder. This part works. The folder is created like so:

os.makedirs(new_folder, 0o777)

new_folder stores the name, given by the user, of the folder to be created.

The next snippet of code is from another script that does the opposite. It takes the files from the new folder and moves them back to the original folder and it does this successfully. However what doesn't work is what is supposed to happen next. Once moved back, it is supposed to delete the new folder with its content. I tried doing it with this code:

os.chdir(new_path)
os.remove(folder_name)

og_path is just a variable that stores the path of the new folder which should be deleted. folder_name stores well...the folder's name

When I run the full code of the second script everything works; however when it reaches:

os.remove(folder_name)

It gives me this error:

Traceback (most recent call last):
  File "/Users/TVM/Desktop/python/move_file/move_file_reverse.py", line  25, in <module>
os.remove(folder_name)
PermissionError: [Errno 1] Operation not permitted: 'lab3'

Additional Variable Information:

 new_folder = "lab3"
 folder_name = "lab3"
 new_path = "/Users/TVM/Desktop/python/move_file/newloc"

The folder called lab3 is in the folder newloc

TVM
  • 161
  • 1
  • 1
  • 6
  • 1
    Does the folder still contain files? If so, I believe that is the problem; you have to get rid of all the files first. – John Gordon Oct 03 '17 at 02:34
  • 1
    This isn't a [MCVE]; your variable names aren't consistent, and it's unclear what other code might be occurring to define said names, or the relationship between them, or what other operations might be being performed on the file system in between. – ShadowRanger Oct 03 '17 at 02:35
  • 1
    Following up on @JohnGordon: If you're trying to remove a whole directory tree, take a look [`shutil.rmtree`](https://docs.python.org/3/library/shutil.html#shutil.rmtree) which is a higher level function for deleting a directory and all of its contents. – ShadowRanger Oct 03 '17 at 02:36
  • @JohnGordon I have tried getting rid of the files first however it still gives me the error on the file – TVM Oct 03 '17 at 02:38
  • _I have tried getting rid of the files first_ Saying you "tried" implies you weren't successful. Did you remove the files, or not? – John Gordon Oct 03 '17 at 02:42
  • @ShadowRanger I apologize for that I didn't think it would be relevant. I will try and add what I believe you say I am missing. As a clarification, if I were to us shutil.rmtree would that only remove the directory. For example if "/user/tvm/folder/" is the file true and I use shutil.rmtree, will it delete the contents of folder of will it delete everything? – TVM Oct 03 '17 at 02:43
  • @JohnGordon the files within the folder were successfully removed however the folder itself was not. – TVM Oct 03 '17 at 02:48
  • Show us the output of `ls -la /Users/TVM/Desktop/python/move_file/newloc` – John Gordon Oct 03 '17 at 02:51
  • @JohnGordon the output after which point? After I try to remove the files? – TVM Oct 03 '17 at 02:54
  • Yes, when the directory should be empty. – John Gordon Oct 03 '17 at 02:57
  • @JohnGordon the suggestion by ShadowRanger to use shutil.rmtree() and this worked but thank you. – TVM Oct 03 '17 at 03:00

2 Answers2

19

You should use os.rmdir instead of os.remove.

    os.mkdir('mydir')
    os.rmdir('mydir')
    os.path.exists('mydir')

In case if the directory is not empty and you would like to get rid of the whole directory tree starting from the directory you should use:

shutil.rmtree('mydir')
  • OSError: [Errno 66] Directory not empty: – Jürgen K. Dec 03 '21 at 07:23
  • 1
    I edited my post. In such case if you would like to get rid of the whole directory tree you should go with `shutil.rmtree` – Paweł Kozielski-Romaneczko Dec 07 '21 at 11:56
  • Still doesn't work: FileNotFoundError: [Errno 2] No such file or directory: '._image1.jpg'. please check my question: https://stackoverflow.com/questions/70246591/delete-directory-and-all-symlinks-recursively?noredirect=1#comment124177383_70246591 – Jürgen K. Dec 07 '21 at 13:57
  • Its hard to reproduce this problem. When I am trying to create a directory structure which would raise this error I can't do it (tested with symbolic links, jpg files, hidden files and so on). This would be probably key to handle this, understand which particular subdirectory or file is causing the problems. – Paweł Kozielski-Romaneczko Dec 08 '21 at 11:45
  • I found out that whereever symbolic links come from, rmtree cant remove those. so i'm searching for another solution – Jürgen K. Dec 08 '21 at 12:45
6

In the comments @ShadowRanger suggest to use shutil.rmtree()

I replaced os.remove() with shutil.rmtree() and it worked. Thank you very much @ShadowRanger.

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
TVM
  • 161
  • 1
  • 1
  • 6