Wednesday, April 27, 2022

[SOLVED] chmod not working for pytest folder generation

Issue

I am trying to write to a folder generated by Python. Python has generated the folder as read only, and research suggests i should use os.chmod('Dump',0o777) to fix the permissions. This does not work for me, and I cannot copy into the generated folder.

I am using pytest, and my setup module is:

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)

And the test starts with:

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

The error that comes up is:

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

Is there something obvious I am missing?


Solution

Won't this line try to open the directory "Dump" and write to it like a file?

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

Maybe if you pass the entire path, like

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


Answered By - con--
Answer Checked By - David Marino (WPSolving Volunteer)