
import contextlib
import os
import pathlib
from zipfile import ZipFile


def main():
    suffixes = '01', '02'
    tuple(map(generate, suffixes))


def generate(suffix):
    basepath = pathlib.Path('ziptestdata')
    base = pathlib.Path('importlib_resources/tests')
    zfpath = base / f'zipdata{suffix}/ziptestdata.zip'
    with ZipFile(zfpath, 'w') as zf:
        relpath = base / f'data{suffix}'
        for dirpath, dirnames, filenames in os.walk(relpath):
            with contextlib.suppress(KeyError):
                dirnames.remove('__pycache__')
            for filename in filenames:
                src = os.path.join(dirpath, filename)
                if src == zfpath:
                    continue
                commonpath = os.path.commonpath((relpath, dirpath))
                dst = basepath / dirpath[len(commonpath) + 1 :] / filename
                print(src, '->', dst)
                zf.write(src, dst)


__name__ == '__main__' and main()


import contextlib
import os
import pathlib
from zipfile import ZipFile


def main():
    suffixes = '01', '02'
    tuple(map(generate, suffixes))


def generate(suffix):
    basepath = pathlib.Path('ziptestdata')
    base = pathlib.Path('importlib_resources/tests')
    zfpath = base / f'zipdata{suffix}/ziptestdata.zip'
    with ZipFile(zfpath, 'w') as zf:
        datapath = base / f'data{suffix}'
        for dirpath, dirnames, filenames in os.walk(datapath):
            with contextlib.suppress(KeyError):
                dirnames.remove('__pycache__')
            loc = pathlib.Path(dirpath).relative_to(datapath)
            for filename in filenames:
                src = os.path.join(dirpath, filename)
                if src == zfpath:
                    continue
                dst = basepath / loc / filename
                print(src, '->', dst)
                zf.write(src, dst)


__name__ == '__main__' and main()

Use relative_to instead of string manipulation
Use relative_to instead of string manipulation

  
  import contextlib
  import os
  import pathlib
  from zipfile import ZipFile
  
  
  def main():
      suffixes = '01', '02'
      tuple(map(generate, suffixes))
  
  
  def generate(suffix):
      basepath = pathlib.Path('ziptestdata')
      base = pathlib.Path('importlib_resources/tests')
      zfpath = base / f'zipdata{suffix}/ziptestdata.zip'
      with ZipFile(zfpath, 'w') as zf:
-         relpath = base / f'data{suffix}'
+         datapath = base / f'data{suffix}'
-         for dirpath, dirnames, filenames in os.walk(relpath):
+         for dirpath, dirnames, filenames in os.walk(datapath):
              with contextlib.suppress(KeyError):
                  dirnames.remove('__pycache__')
+             loc = pathlib.Path(dirpath).relative_to(datapath)
              for filename in filenames:
                  src = os.path.join(dirpath, filename)
                  if src == zfpath:
                      continue
-                 commonpath = os.path.commonpath((relpath, dirpath))
-                 dst = basepath / dirpath[len(commonpath) + 1 :] / filename
+                 dst = basepath / loc / filename
                  print(src, '->', dst)
                  zf.write(src, dst)
  
  
  __name__ == '__main__' and main()
  
Use relative_to instead of string manipulation
## Code Before:

import contextlib
import os
import pathlib
from zipfile import ZipFile


def main():
    suffixes = '01', '02'
    tuple(map(generate, suffixes))


def generate(suffix):
    basepath = pathlib.Path('ziptestdata')
    base = pathlib.Path('importlib_resources/tests')
    zfpath = base / f'zipdata{suffix}/ziptestdata.zip'
    with ZipFile(zfpath, 'w') as zf:
        relpath = base / f'data{suffix}'
        for dirpath, dirnames, filenames in os.walk(relpath):
            with contextlib.suppress(KeyError):
                dirnames.remove('__pycache__')
            for filename in filenames:
                src = os.path.join(dirpath, filename)
                if src == zfpath:
                    continue
                commonpath = os.path.commonpath((relpath, dirpath))
                dst = basepath / dirpath[len(commonpath) + 1 :] / filename
                print(src, '->', dst)
                zf.write(src, dst)


__name__ == '__main__' and main()

## Instruction:
Use relative_to instead of string manipulation
## Code After:

import contextlib
import os
import pathlib
from zipfile import ZipFile


def main():
    suffixes = '01', '02'
    tuple(map(generate, suffixes))


def generate(suffix):
    basepath = pathlib.Path('ziptestdata')
    base = pathlib.Path('importlib_resources/tests')
    zfpath = base / f'zipdata{suffix}/ziptestdata.zip'
    with ZipFile(zfpath, 'w') as zf:
        datapath = base / f'data{suffix}'
        for dirpath, dirnames, filenames in os.walk(datapath):
            with contextlib.suppress(KeyError):
                dirnames.remove('__pycache__')
            loc = pathlib.Path(dirpath).relative_to(datapath)
            for filename in filenames:
                src = os.path.join(dirpath, filename)
                if src == zfpath:
                    continue
                dst = basepath / loc / filename
                print(src, '->', dst)
                zf.write(src, dst)


__name__ == '__main__' and main()
