Reputation: 12357
This is an easy question.
I'm using glob to print the full hierarchy of a folder using this code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import argparse
import sys
import glob
parser = argparse.ArgumentParser()
parser.add_argument('-input', dest='input',help="input one or more files",metavar=None)
args = parser.parse_args()
def dirlist(path):
for i in glob.glob(os.path.join(path, "*")):
if os.path.isfile(i):
print (i)
elif os.path.isdir(i):
dirname = os.path.basename(i)
dirlist(i)
path = os.path.normpath(args.input)
dirlist(path)
It works pretty well, you just need to run python3 Test.py -input .
.
As you can see by the argparse
help description I would like to input directories but also single files.
I don't think this can be done using glob
, is that right?
Can you suggest me a library that could help me print the full hierarchy of both directories and files?
I found here a long list of globe
examples but they all seems to work for directories, not when you input a single file
Upvotes: 0
Views: 1514
Reputation: 12357
Is nearly midnight but at least I found the solution after 2 days:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import argparse
import sys
import glob
parser = argparse.ArgumentParser()
parser.add_argument('-input', dest='input',help="input one or more files",metavar=None)
args = parser.parse_args()
def dirlist(path):
if os.path.isfile(path):
print (path)
elif os.path.isdir(path):
for i in glob.glob(os.path.join(path, "*")):
if os.path.isfile(i):
print (i)
elif os.path.isdir(i):
dirname = os.path.basename(i)
dirlist(i)
path = os.path.normpath(args.input)
dirlist(path)
Upvotes: 1