添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
大鼻子的课本  ·  javascript - 找不到 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to rename a file, but if the file name already exists to just move on. The script stops with FileExistsError exception raised, although I think I'm telling it to look for that? Yes, when the exception is raised the file does already exist.

# rename the file os.rename(infilename, newname) except FileExistsError: # output if it exists already print(f'{newname} already exists')

Traceback is:

Exception has occurred: FileExistsError
[WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\MacalusoC\\Desktop\\PNC_to_Evo\\Evo DECO-10\\Evo PROG6001  FSJD0250240M  E.DBP' -> 'C:\\Users\\MacalusoC\\Desktop\\PNC_to_Evo\\Evo DECO-10\\Evo PROG6001  FSJD0250240M  E.part'
  File "C:\Users\MacalusoC\Desktop\PNC_to_Evo\PNC_Deco_to_Evo_Deco.py", line 75, in purge_DBP_files
    os.rename(infilename, newname)
  File "C:\Users\MacalusoC\Desktop\PNC_to_Evo\PNC_Deco_to_Evo_Deco.py", line 341, in main
    purge_DBP_files(new_folder)
  File "C:\Users\MacalusoC\Desktop\PNC_to_Evo\PNC_Deco_to_Evo_Deco.py", line 350, in <module>
    main()
                i ran your code and the dst file exist and it works. please update with Python version and the full Traceback ( just remove the try-except and run your script again, paste the output to here
– ddor254
                Feb 19, 2019 at 15:48
                I've added the traceback. I also ran the block without the try & except statement and the same exception gets raised
– Chris Macaluso
                Feb 19, 2019 at 17:18
  

os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None) Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file. [1] https://docs.python.org/3/library/os.html

So either, the exception is not thrown because you are on a unix system or you are catching the wrong exception.

Why not flip your logic? It's often better to check if something can be done first rather than try it and see if it fails.

from os import path, rename
if not path.exists(newname):
    rename(infilename, newname)
                That's the opposite of normal advice. It's almost always better to try and fail rather than test then try. Trying and failing is atomic. Splitting up testing and trying opens you up to race conditions; what if the file is created after you test but before you rename? In this particular case it may be okay, but in general it's bad advice.
– John Kugelman
                Feb 19, 2019 at 16:28
def rename_file(current_file_name, new_file_name):
    """ rename a file if 'current_file_name' exists and 'new_file_name' doesnt exist 
    :param current_file_name: 
    :param new_file_name: 
    :return: 
    if os.path.exists(new_file_name) or not os.path.exists(current_file_name):
        return
    else:
        os.rename(current_file_name, new_file_name)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.