Copy a file with a too long path to another directory in Python

前端 未结 3 561
余生分开走
余生分开走 2020-12-14 10:20

I am trying to copy files on Windows with Python 2.7, but sometimes this fails.

shutil.copyfile(copy_file, dest_file)

I get the following I

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 10:53

    I wasn't sure about the 255 char limit so I stumbled on this post. There I found a working answer: adding \\?\ before the path.

    shutil.copyfile("\\\\?\\" + copy_file, dest_file)
    

    edit: I've found that working with long paths causes issues on Windows. Another trick I use is to just shorten the paths:

    import win32api
    path = win32api.GetShortPathName(path)
    

提交回复
热议问题