os.rename 文件存在时不会引发 FileExistsError
os.rename does not raise FileExistsError when file exists
我有一个 file_rename
机制,我想用一个简单的 try/except
块来改进它,该块将检查重命名的文件是否已经存在于目录中。
我在目录中准备了2个文件:data.txt
和old_data.txt
。函数应该抛出异常,因为 old_data.txt
已经存在,这意味着过去处理过数据。
但是,下面的代码不起作用,因为它仍在重命名 data.txt
文件。如果能提供任何帮助和指导,我将不胜感激。
@staticmethod
def file_rename(file, dir):
source_file_new_name = "old_" + file
try:
os.path.isfile(dir + source_file_new_name)
os.rename(os.path.join(dir, file), os.path.join(dir, source_file_new_name))
except FileExistsError:
raise FileExistsError("this file was already processed")
根据 Rafał 和 BrokenBenchmark 的提示,我想出了以下版本,但不确定它是否足够 pythonic ;)
class FileExistsError(Exception):
pass
@staticmethod
def file_rename(file, dir):
source_file_new_name = "old_" + file
if os.path.isfile(dir + source_file_new_name):
raise FileExistsError("this file was already processed!")
else:
os.rename(os.path.join(dir, file), os.path.join(dir, source_file_new_name))
os.path.isfile 方法只是 returns True(如果文件存在)或 False(如果不存在)。在调用 os.rename.
之前使用 if 语句检查其结果
您的代码假定 os.rename
将引发 FileExistsError
。 只有当代码在 Windows 上 运行 时才能做出该假设。 The Python docs for os.rename()
state:
os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Rename the file or directory src to dst. If dst exists, the operation will fail with an OSError
subclass in a number of cases:
On Windows, if dst exists a FileExistsError
is always raised.
On Unix, ... [i]f both [src and dst] are files, dst it [sic] will be replaced silently if the user has permission.
要解决此问题,请改用带有 .isfile()
的 if
语句,而不是依赖依赖于不可移植行为的 try/except
才能工作:
def file_rename(file, dir):
source_file_new_name = "old_" + file
if os.path.isfile(os.path.isfile(dir + source_file_new_name)):
os.rename(os.path.join(dir, file), os.path.join(dir, source_file_new_name))
我有一个 file_rename
机制,我想用一个简单的 try/except
块来改进它,该块将检查重命名的文件是否已经存在于目录中。
我在目录中准备了2个文件:data.txt
和old_data.txt
。函数应该抛出异常,因为 old_data.txt
已经存在,这意味着过去处理过数据。
但是,下面的代码不起作用,因为它仍在重命名 data.txt
文件。如果能提供任何帮助和指导,我将不胜感激。
@staticmethod
def file_rename(file, dir):
source_file_new_name = "old_" + file
try:
os.path.isfile(dir + source_file_new_name)
os.rename(os.path.join(dir, file), os.path.join(dir, source_file_new_name))
except FileExistsError:
raise FileExistsError("this file was already processed")
根据 Rafał 和 BrokenBenchmark 的提示,我想出了以下版本,但不确定它是否足够 pythonic ;)
class FileExistsError(Exception):
pass
@staticmethod
def file_rename(file, dir):
source_file_new_name = "old_" + file
if os.path.isfile(dir + source_file_new_name):
raise FileExistsError("this file was already processed!")
else:
os.rename(os.path.join(dir, file), os.path.join(dir, source_file_new_name))
os.path.isfile 方法只是 returns True(如果文件存在)或 False(如果不存在)。在调用 os.rename.
之前使用 if 语句检查其结果您的代码假定 os.rename
将引发 FileExistsError
。 只有当代码在 Windows 上 运行 时才能做出该假设。 The Python docs for os.rename()
state:
os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Rename the file or directory src to dst. If dst exists, the operation will fail with an
OSError
subclass in a number of cases:On Windows, if dst exists a
FileExistsError
is always raised.On Unix, ... [i]f both [src and dst] are files, dst it [sic] will be replaced silently if the user has permission.
要解决此问题,请改用带有 .isfile()
的 if
语句,而不是依赖依赖于不可移植行为的 try/except
才能工作:
def file_rename(file, dir):
source_file_new_name = "old_" + file
if os.path.isfile(os.path.isfile(dir + source_file_new_name)):
os.rename(os.path.join(dir, file), os.path.join(dir, source_file_new_name))