Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include entire directory in python setup.py data_files

The data_files parameter for setup takes input in the following format:

setup(...
    data_files = [(target_directory, [list of files to be put there])]
    ....)

Is there a way for me to specify an entire directory of data instead, so I don't have to name each file individually and update it as I change implementation in my project?

I attempted to use os.listdir(), but I don't know how to do that with relative paths, I couldn't use os.getcwd() or os.realpath(__file__) since those don't point to my repository root correctly.

like image 982
disambiguator Avatar asked Jan 07 '15 22:01

disambiguator


2 Answers

karelv has the right idea, but to answer the stated question more directly:

from glob import glob

setup(
    #...
    data_files = [
        ('target_directory_1', glob('source_dir/*')), # files in source_dir only - not recursive
        ('target_directory_2', glob('nested_source_dir/**/*', recursive=True)), # includes sub-folders - recursive
        # etc...
    ],
    #...
)
like image 155
Pete Baughman Avatar answered Oct 13 '22 23:10

Pete Baughman


import glob

for filename in glob.iglob('inner_dir/**/*', recursive=True):
    print (filename)

Doing this, you get directly a list of files relative to current directory.

like image 23
karelv Avatar answered Oct 13 '22 23:10

karelv