OSError:目录不是空的提出,如何解决?(OSError: Directory not empty raised, how to fix?)

编程入门 行业动态 更新时间:2024-10-27 02:18:36
OSError:目录不是空的提出,如何解决?(OSError: Directory not empty raised, how to fix?)

我只是试图编写一个小应用程序,它从名为'DATA.DAT'的文件中获取一个值,并用该值重命名包含该文件的文件夹。

.py脚本运行在另一个文件夹中,并允许用户定义路径。 为了给你一个更好的想法,用户定义的路径必须是(在Mac上)'/ Users / User / Desktop / FOLDER','FOLDER'应该包含'DATA.DAT'。

这就是源代码的一小部分:

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()

那么,它的工作; 但是当“FOLDER”包含多个文件时(实际上,“FOLDER”应该只包含'DATA.DAT'和其他文件夹,这并不会产生问题)会引发异常。

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

只是为了防止这种情况发生,有没有办法解决这个问题? 谢谢。

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.

最满意答案

编辑:正确的工具是shutil.move :

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

假设path_paths[-1]是要重命名的绝对目录, data_data是要重命名的绝对目录名称。

目标目录不能存在才能使用。 这两个位置不需要在同一个文件系统上。


旧答案:使用os.renames而不是os.rename 。

它会递归地创建任何需要的目录。

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.

更多推荐

本文发布于:2023-07-07 15:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1065201.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何解决   目录   OSError   Directory   fix

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!