【问题标题】:chmod not working for pytest folder generationchmod 不适用于 pytest 文件夹生成
【发布时间】:2018-03-10 04:43:42
【问题描述】:

我正在尝试写入 Python 生成的文件夹。 Python 已将文件夹生成为只读,研究建议我应该使用os.chmod('Dump',0o777) 来修复权限。这对我不起作用,我无法复制到生成的文件夹中。

我正在使用 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

我有什么明显的遗漏吗?

【问题讨论】:

    标签: python permissions pytest chmod


    【解决方案1】:

    这行不会尝试打开目录“Dump”并像文件一样写入吗?

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

    也许如果你通过整个路径,比如

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

    【讨论】:

    • 解决了,完美。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    相关资源
    最近更新 更多