【问题标题】:Python. IOError: [Errno 13] Permission denied: when i'm copying filePython。 IOError:[Errno 13] 权限被拒绝:当我复制文件时
【发布时间】:2011-11-23 00:02:30
【问题描述】:

我有两个文件夹:In,Out - 它不是磁盘 D 上的系统文件夹: - Windows 7。Out 包含“myfile.txt” 我在 python 中运行以下命令:

>>> shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )

Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )
  File "C:\Python27\lib\shutil.py", line 82, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: 'D:\\In'

有什么问题?

【问题讨论】:

  • 使用资源管理器我可以将 myfile.txt 复制到文件夹中

标签: python windows windows-7


【解决方案1】:

阅读docs

shutil.copyfile(src, dst)

将名为src的文件的内容(无元数据)复制到一个文件中 命名为 dstdst 必须是完整的目标文件名;看copy() 对于接受目标目录路径的副本。

【讨论】:

  • 我尝试了shutil.copy,但仍然面临同样的错误。
  • 我认为正如 Tim 所说,它应该是完整的路径,意思是 shutil.copyfile(src, dst) // 源和目标也应该包括目录和文件名。
【解决方案2】:

使用 shutil.copy 而不是 shutil.copyfile

示例:

shutil.copy(PathOf_SourceFileName.extension,TargetFolderPath)

【讨论】:

    【解决方案3】:

    对于 Python 3.6 的新查看器来说,这个问题已经很老了 使用

    shutil.copyfile( "D:\Out\myfile.txt", "D:\In" )
    

    而不是

    shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )
    

    r 参数用于读取文件而不是用于复制

    【讨论】:

    • 这个答案是错误的。 r 表示原始字符串,意思是字符串中的“\”字面意思是“\”,不需要转义。
    【解决方案4】:

    我解决了这个问题,你应该是destination的完整目标文件名

    目的地 = 路径目录 + 文件名。*

    我将这段代码与 shutil 一起使用 fir 复制 wav 文件:

        # open file with QFileDialog
    
        browse_file = QFileDialog.getOpenFileName(None, 'Open file', 'c:', "wav files (*.wav)")
    
        # get file name 
    
        base = os.path.basename(browse_file[0])
        os.path.splitext(base)
        print(os.path.splitext(base)[1])
    
        # make destination path with file name
    
        destination= "test/" + os.path.splitext(base)[0] + os.path.splitext(base)[1]
        shutil.copyfile(browse_file[0], destination)
    

    【讨论】:

      【解决方案5】:

      使用 shutil.copy2 代替 shutil.copyfile

      import shutil 
      shutil.copy2('/src/dir/file.ext','/dst/dir/newname.ext') # file copy to another file
      shutil.copy2('/src/file.ext', '/dst/dir') # file copy to diff directory
      

      【讨论】:

        【解决方案6】:

        使用

        > from shutil import copyfile
        > 
        > copyfile(src, dst)
        

        供 src 和 dst 使用:

        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        

        【讨论】:

          【解决方案7】:

          首先,确保您的文件没有被 Windows 锁定,某些应用程序(例如 MS Office)会锁定打开的文件。

          当我试图重命名目录中的长文件列表时出现错误 13,但 Python 试图重命名与我的文件位于同一路径的某些文件夹。所以,如果你没有使用shutil库,请检查它是目录还是文件!

          import os
          path="abc.txt"
          
          if os.path.isfile(path):
              #do yor copy here
              print("\nIt is a normal file") 
          

          或者

          if os.path.isdir(path):
              print("It is a directory!")
          else:
              #do yor copy here
              print("It is a file!")
          

          【讨论】:

            【解决方案8】:

            这对我有用:

            import os
            import shutil
            import random
            
            
            dir = r'E:/up/2000_img'
            output_dir = r'E:/train_test_split/out_dir'
            
            
            files = [file for file in os.listdir(dir) if os.path.isfile(os.path.join(dir, file))]
            
            if len(files) < 200:
                # for file in files:
                #     shutil.copyfile(os.path.join(dir, file), dst)
                pass
            else:
                # Amount of random files you'd like to select
                random_amount = 10
                for x in range(random_amount):
                    if len(files) == 0:
                        break
                    else:
                        file = random.choice(files)
                        shutil.copyfile(os.path.join(dir, file), os.path.join(output_dir, file))
            

            【讨论】:

              【解决方案9】:

              确保您没有处于(锁定)您尝试使用 shutil.copy 的任何文件中。

              这应该有助于解决您的问题

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2011-06-11
                • 2013-05-02
                • 1970-01-01
                • 2017-11-05
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多