簡體   English   中英

Python.IOError: [Errno 13] Permission denied: 當我復制文件時

[英]Python. IOError: [Errno 13] Permission denied: when i'm copying file

我有兩個文件夾: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'

有什么問題?

閱讀文檔

shutil.copyfile(src, dst)

將名為src的文件的內容(無元數據)復制到名為dst的文件中。 dst必須是完整的目標文件名 查看copy()以獲得接受目標目錄路徑的副本。

使用 shutil.copy 而不是 shutil.copyfile

例子:

shutil.copy(PathOf_SourceFileName.extension,TargetFolderPath)

我解決了這個問題,you should be the complete target file name for destination

目的地 = 路徑目錄 + 文件名。*

我使用此代碼 fir 使用 shutil 復制 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)

使用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

首先,請確保您的文件未被 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!")

視覺工作室 2019

解決方案:管理員提供了對正在運行的文件夾“C:\ProgramData\Docker”的完全訪問權限。

錯誤:文件 IO 將文件復制到卷時出現錯誤:edgehubdev。 錯誤號:13,錯誤權限被拒絕:[錯誤號 13] 權限被拒絕:'C:\ d "C:\Users\radhe.sah\source\repos\testing\AzureIotEdgeApp1\config\deployment.windows-amd64.json" -v' 錯誤:警告。 通過 CLI 使用 --password 是不安全的。 使用 --password-stdin: ERROR: File IO 將文件復制到卷時出現錯誤。 edgehubdev:Errno,13:錯誤權限被拒絕:[Errno 13] 權限被拒絕:'C.\ProgramData\Docker\volumes\edgehubdev\_data\edge-chain-ca.cert.pem'

采用

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

對於 src 和 dst 使用:

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

這對我有用:

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))

確保您不在(鎖定)您嘗試使用 shutil.copy 的任何文件中。

這應該有助於解決您的問題

確保您之前確實關閉了目標文件

我通過這樣做避免了這個錯誤:

  1. 導入 lib 'pdb' 並在 'shutil.copyfile' 之前插入 'pdb.set_trace()',它就像這樣:
  import pdb
    ...
    print(dst)
    pdb.set_trace()
    shutil.copyfile(src,dst)
  1. 在終端中運行 python 文件,它將執行到“pdb.set_trace()”行,現在將打印出“dst”文件。

  2. 自己復制'src'文件,並替換並刪除上面代碼創建的'dst'文件。

  3. 然后輸入'c'並點擊終端中的'Enter'鍵來執行下面的代碼。

好吧,問題是老問題,供 Python 3.6 使用的新觀眾使用

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

代替

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

r參數被傳遞用於讀取文件而不是用於復制

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM