Python:复制长文件路径Shutil.copyfile [英] Python: copy long file path Shutil.copyfile

查看:107
本文介绍了Python:复制长文件路径Shutil.copyfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用shutil.copyfile 用python 复制太长的路径.

I want to copy too long paths with python using shutil.copyfile.

现在我读了这个将路径过长的文件复制到Python中的另一个目录页面获取解决方案.我用过:

Now I read this Copy a file with a too long path to another directory in Python page to get the solution. I used:

    shutil.copyfile(r'\\\\?\\' +  ErrFileName,testPath+"\\"+FilenameforCSV+"_lyrErrs"+timestrLyr+".csv")

复制文件但它给了我一个错误:[Errno 2] 没有这样的文件或目录:'\\\\?\\C:\\...

to copy the file but it gives me an error : [Errno 2] No such file or directory: '\\\\?\\C:\\...

谁能告诉我如何将长路径与 Shutil.copyfile 合并,我上面使用的方法应该允许在文件路径中包含 32k 个字符,但我什至无法达到 1000,它给了我这个错误.

Can anyone please let me know how to incorporate longs paths with Shutil.copyfile, the method I used above should allow 32k characters inside a file path, but I cant even reach 1000 and it gives me this error.

推荐答案

由于\\?\前缀绕过了正常的路径处理,路径需要是绝对的,只能使用反斜杠作为路径分隔符,并且必须是 UTF-16 字符串.在 Python 2 中,使用 u 前缀创建一个 unicode 字符串(Windows 上的 UTF-16).

Since the \\?\ prefix bypasses normal path processing, the path needs to be absolute, can only use backslash as the path separator, and has to be a UTF-16 string. In Python 2, use the u prefix to create a unicode string (UTF-16 on Windows).

shutil.copyfile'rb' 模式打开源文件,以'wb' 模式打开目标文件,然后从源文件复制以 16 KiB 块的形式到达目的地.给定 unicode 路径,Python 2 通过调用 C 运行时函数 _wfopen 打开一个文件,该函数又调用 Windows 宽字符 API CreateFileW.

shutil.copyfile opens the source file in 'rb' mode and destination file in 'wb' mode, and then copies from source to destination in 16 KiB chunks. Given a unicode path, Python 2 opens a file by calling the C runtime function _wfopen, which in turn calls the Windows wide-character API CreateFileW.

shutil.copyfile 应该适用于长路径,假设它们的格式正确.如果它对您不起作用,我想不出任何强制"它起作用的方法.

shutil.copyfile should work with long paths, assuming they're correctly formatted. If it's not working for you, I can't think of any way to "force" it to work.

这是一个 Python 2 示例,它创建了一个 10 级目录树,每个目录树名为 u'a' * 255,并将文件从工作目录复制到树的叶节点.目标路径大约为 2600 个字符,具体取决于您的工作目录.

Here's a Python 2 example that creates a 10-level tree of directories, each named u'a' * 255, and copies a file from the working directory into the leaf of the tree. The destination path is about 2600 characters, depending on your working directory.

#!python2
import os
import shutil

work = 'longpath_work'
if not os.path.exists(work):
    os.mkdir(work)
os.chdir(work)

# create a text file to copy
if not os.path.exists('spam.txt'):
    with open('spam.txt', 'w') as f:
        f.write('spam spam spam')

# create 10-level tree of directories
name = u'a' * 255
base = u'\\'.join([u'\\\\?', os.getcwd(), name])
if os.path.exists(base):
    shutil.rmtree(base)
rest = u'\\'.join([name] * 9)
path = u'\\'.join([base, rest])
os.makedirs(path)

print 'src directory listing (tree created)'
print os.listdir(u'.')

dest = u'\\'.join([path, u'spam.txt'])
shutil.copyfile(u'spam.txt', dest)
print '\ndest directory listing'
print os.listdir(path)

print '\ncopied file contents'
with open(dest) as f:
    print f.read()

# Remove the tree, and verify that it's removed:
shutil.rmtree(base)
print '\nsrc directory listing (tree removed)'
print os.listdir(u'.')

输出(换行):

src directory listing (tree created)
[u'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   aaaaa', u'spam.txt']

dest directory listing
[u'spam.txt']

copied file contents
spam spam spam

src directory listing (tree removed)
[u'spam.txt']

这篇关于Python:复制长文件路径Shutil.copyfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆