OSError: Directory not empty raised, how to fix?
Asked Answered
F

3

6

I'm just trying to write a little application that takes a value from a file named 'DATA.DAT' and renames the folder which contains that file with that value.

The .py script runs in another folder and allows the user to define the path. To give you a better idea, the user defined path must be like (on a mac) '/Users/User/Desktop/FOLDER' and 'FOLDER' should contain 'DATA.DAT'.

That's how a little part of the source code looks like:

try:
    data = open('DATA.DAT').read()

    data_data = data[12:17]
    path_paths = path.rsplit('/')
    basepath = '/'.join(path_paths[:-1])
    chdir(basepath)

    if path_paths[-1] <> data_data:
        rename(path_paths[-1], data_data)
        raw_input('Folder name has been corrected! Thank you.')
        quit()
    else:
        print('Folder name was already correct! Thank you.')
        quit()
except IndexError:
    raw_input('ERROR!')
    quit()

Well, it works; but it raise and exception when 'FOLDER' contains more than one file (actually, 'FOLDER' should contain just 'DATA.DAT' and other folders. That doesn't give problems.)...

Traceback (most recent call last):
  File "/Users/User/Desktop/example.py", line 72, in <module>
    rename(path_paths[-1], data_data)
OSError: [Errno 66] Directory not empty

Just to prevent that this happens, is there a way to fix it? Thanks.

Fatness answered 20/9, 2011 at 14:59 Comment(1)
What happens when you chdir() out of the directory before renaming it? What platform is this?Impresa
D
15

Edit: The right tool is shutil.move:

shutil.move(path_paths[-1], data_data)

assuming path_paths[-1] is the absolute directory you want to rename, and data_data is the absolute directory name you want to rename it to.

The destination directory must not already exist for this to work. The two locations don't need to be on the same filesystem.


Old answer: Use os.renames instead of os.rename.

It will recursively create any needed directories.

Dulciedulcify answered 20/9, 2011 at 15:13 Comment(3)
I've tryed but it gives me exactly the same error: Traceback (most recent call last): File "/Users/User/Desktop/example.py", line 72, in <module> renames(path_paths[-1], data_data) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 199, in renames rename(old, new) OSError: [Errno 66] Directory not emptyFatness
Thank you, now it's working! Here an example: path_paths[-1] = '/Users/User/Desktop/lol' data_data = '/Users/User/Desktop/asd' ...am I right?Fatness
@Fatness that looks right to me, as long as the asd directory doesn't already exist.Dulciedulcify
B
1

It is much easier to use shutil.

Barrel answered 20/9, 2011 at 16:16 Comment(0)
E
0

Althoug a decade later.. is possible to replace the way for split path

path_paths = path.rsplit('/')

for

path_paths = os.path.split(path)[1]

And for renaming problem:

os.rename('myfolder/oldname', 'myfolder/newname')
Ec answered 3/9, 2020 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.