2017-09-28 38 views
0

我正在嘗試寫入由Python生成的文件夾。 Python已經將該文件夾生成爲只讀文件,研究表明我應該使用os.chmod('Dump',0o777)來修復權限。這對我不起作用,我無法複製到生成的文件夾中。chmod不能用於pytest文件夾生成

我使用pytest,和我的設置模塊:

def setup_module(module): 
    """setup the environment for the tests to occur in, 
    which involves generating the ini files that will 
    be used. """ 
    os.system('mkdir Dump') 
    os.chmod('Dump',0o777) 

而且測試始於:

def test_archive_large_zips(): 
    shutil.copyfile('Logs\\MultiPRMLogs.zip','Dump') 

,來了的錯誤是:

src = '\\Logs\\MultiPRMLogs.zip', dst = 'Dump' 
def copyfile(src, dst, *, follow_symlinks=True): 
    """Copy data from src to dst. 

    If follow_symlinks is not set and src is a symbolic link, a new 
    symlink will be created instead of copying the file it points to. 

    """ 
    if _samefile(src, dst): 
     raise SameFileError("{!r} and {!r} are the same file".format(src, dst)) 

    for fn in [src, dst]: 
     try: 
      st = os.stat(fn) 
     except OSError: 
      # File most likely does not exist 
      pass 
     else: 
      # XXX What about other special files? (sockets, devices...) 
      if stat.S_ISFIFO(st.st_mode): 
       raise SpecialFileError("`%s` is a named pipe" % fn) 

    if not follow_symlinks and os.path.islink(src): 
     os.symlink(os.readlink(src), dst) 
    else: 
     with open(src, 'rb') as fsrc: 
      with open(dst, 'wb') as fdst: 
E    PermissionError: [Errno 13] Permission denied: 'Dump' 

C:\Users\pet172\AppData\Local\Programs\Python\Python36-32\lib\shutil.py:121: Per 
missionError 

是有什麼明顯的我失蹤?

回答

1

這行不會嘗試打開「轉儲」目錄並將其寫入文件中嗎?

with open(dst, 'wb') as fdst: 

也許如果你傳遞整個路徑,像

shutil.copyfile('Logs\\MultiPRMLogs.zip','Dump\\some_file.txt') 
+1

問題解決了,完美。謝謝 –