What is difference between root and base directory?
Asked Answered
T

4

6

I am trying to use shutils.py , make_archive function.
here: https://docs.python.org/2/library/shutil.html#archiving-operations
but I can't understand the difference between root_dir and base_dir.
Here's a simple code using make_archive:

#!user/bin/python
from os import path
from os import curdir
from shutil import make_archive
# Setting current Directory
current = path.realpath(curdir)
# Now Compressing
make_archive("Backup", "gztar", current)

This will create an archive named Backup.tar.gz which contains a . Directory inside it. I don't want the . directory but the whole thing inside in the archive.

Tijuanatike answered 25/10, 2015 at 18:58 Comment(0)
W
11

root_dir refers to base directory of output file, or working directory for your working script.

base_dir refers to content you want pack.

For example, if you have a directory tree like:

/home/apast/git/someproject

And you want to build a package for someproject folder, you can set:

root_dir="/home/apast/git"
base_dir="someproject"

If the contents of your tree is like following, for example: /home/apast/git/someproject/test.py /home/apast/git/someproject/model.py

The content of your package will acquire following structure:

someproject/test.py someproject/model.py

And your package file will be stored at:

/home/apast/git/<packfile-name>

Like doc shows, by default, root_dir and base_dir are initialized for your current working directory (cwd, or curdir). But, you can use it in a more flexible way.

Wilburnwilburt answered 25/10, 2015 at 19:5 Comment(7)
Thanks a lot got it! any tip on how to get whole directory?Tijuanatike
I fixed my description. these paramters refers to output. Sorry, I believe I started with a wrong solution. I will fix it to clear thingsWilburnwilburt
Which whole directory you want? May you exemplify better the expected result?Wilburnwilburt
actually the description made the answer a bit confusing the expected result is eg: root_dir: /home/usr/project which contains: dir1, dir2, dir3 which means base_dir contains 3 directories. how to add them all?Tijuanatike
I will try something here and put in the answer. Soon I post it hereWilburnwilburt
I will write it inside another answer, because it is a different question.Wilburnwilburt
very helpful, the docs are not very clearAnderegg
W
4

Let's consider following dir structure:

/home/apast/git/web/tornado.py
/home/apast/git/web/setup.py

/home/apast/git/core/service.py

/home/apast/git/mobile/gui.py
/home/apast/git/mobile/restfulapi.py

We will try two snippets to clarify examples: 1. Defining base_dir 2. Without defined base_dir

  1. Defining base_dir, we specify which directory we will include on our file:

    from shutil import make_archive
    root_dir = "/home/apast/git/"
    
    make_archive(base_name="/tmp/outputfile",
                 format="gztar",
                 root_dir=root_dir,
                 base_dir="web")
    

    This code will generate a file called /tmp/outputfile.tar.gz with following struct:

    web/tornado.py
    web/setup.py
    
  2. Running without base_dir, like following:

    from shutil import make_archive
    
    root_dir = "/home/apast/git/"
    
    make_archive(base_name="/tmp/outputfile",
                 format="gztar",
                 root_dir=root_dir)
    

    It will product a file containing:

    web/tornado.py
    web/setup.py
    core/service.py
    mobile/gui.py
    mobile/restfulapi.py
    

To define specific folders, maybe it will be necessary use some other technique, like gzip lib directly.

Wilburnwilburt answered 25/10, 2015 at 19:41 Comment(3)
well I think it needs gzip lib directly as you say. using without base_dir will create a directory like: /. => /web /mobileTijuanatike
Yes. It seens a little bit simple method. Otherwise, shutils is a great lib on dealing with os operations. Let's try something with other libsWilburnwilburt
Hey! Here seens a good solution for your issue! docs.python.org/2/library/tarfile.html#tarfile.TarFile.add and docs.python.org/2/library/tarfile.html#tarfile.TarFile.addfile . Hope it helps you!Wilburnwilburt
E
2

cd in the root_dir... then tar the base_dir... the docs makes me confuse too, read the code, that will make u clear.

Eberhardt answered 25/9, 2017 at 6:48 Comment(0)
L
1

It's a bit confusing if you read the documentation but if you see it visually, it can help quite a bit.

The root_dir is the directory that you will be storing the file in.

If I were storing a file in C:\Users\Elipzer\Desktop\MyFolder\, That would be my root_dir.

The base_dir is the part added onto the root_dir so if I were storing it under my MyFolder in ...\MyFolder\MySubFolder\, I would put that as the base_dir.

In many cases, there is no need to use these since you can just change the default directory to the directory that you want to store the file in and the make_archive function will just use the default directory as the root_dir and base_dir.

Lilian answered 25/10, 2015 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.