Reputation: 173
i'm currently studying the argparse module and i want my program to behave like this:
$ python cldir.py # this should delete all .meta files in ~/downloads
$ python cldir.py -d /path/name/ # delete all .meta files in /path/name
$ python cldir.py -d /path/name -e txt # should delete all .txt files in /path/name
Now, i know i need to use another argument for the file extension (-e in this case), but i would like to know how i can make the -d switch to behave as shown above.
Here is my code so far:
#!/usr/bin/env python
import argparse
import os
import glob
version = "0.1.1"
parser = argparse.ArgumentParser(
description = 'cldir: clear a directory from .meta and other unwanted files')
# arguments list
parser.add_argument('-v', '--version', action='version', version = version)
parser.add_argument('-d', '--direct', action = 'store_true')
args = parser.parse_args()
if args.direct == False:
path = os.environ['HOME'] + '/downloads/'
files = glob.glob(path + '*.meta')
if len(files) == 0:
print('No .meta files found in ~/downloads')
else:
for f in files:
os.remove(f)
print('%d file(s) deleted' %len(files))
else:
print(args.direct)
The else: print(args.direct) is just for testing until i achieve the desired behaviour.
Any other observations/suggestions are highly welcome as well since i'm new to python.
Upvotes: 1
Views: 397
Reputation: 231738
Using two optionals arguments lets your user specify the directory and extension in either order, or specify one, and let the other be the default:
p.add_argument('-d','--dir',default='default_dir')
p.add_argument('-e','--ext',default='meta')
Input: '' produces: Namespace(dir='default_dir', ext='meta')
'-d otherdir' => Namespace(dir='otherdir', ext='meta')
'-d otherdir -e txt' => Namespace(dir='otherdir', ext='txt')
'-e txt' => Namespace(dir='default_dir', ext='txt')
Blocking the use of -e
before or without -d
is possible, but probably not worth the extra code. However it is the default behavior with positionals.
Or the two arguments could be '?' positionals
p.add_argument('dir',default='default_dir',nargs='?')
p.add_argument('ext',default='meta',nargs='?')
Inputs producing the same namespaces would be ''
, 'otherdir'
, 'otherdir txt'
. In this case specifying ext
is contingent on specifying dir
.
I don't think a store_true
-d
argument adds anything to the definition.
Upvotes: 1
Reputation: 880927
Use -d
to let the user supply a path. You can set a default path with the default
parameter. Similarly, you can set the default value for the -e
option to 'meta'
.
import argparse
import os
import glob
version = "0.1.1"
parser = argparse.ArgumentParser(
description = 'cldir: clear a directory from .meta and other unwanted files')
# arguments list
parser.add_argument('-v', '--version', action='version', version = version)
parser.add_argument('-d', '--directory', default=os.path.expanduser('~/downloads'),
help='directory to be cleaned')
parser.add_argument('-e', default='meta', help='extension')
args = parser.parse_args()
print(args)
path = os.path.join(args.directory)
files = glob.glob(path + '*.{}'.format(args.e))
if len(files) == 0:
print('No .meta files found in ~/downloads')
else:
for f in files:
os.remove(f)
print('%d file(s) deleted' %len(files))
Upvotes: 1
Reputation: 532418
Make -d
a simple 'store' argument with a default value.
parser.add_argument('--direct', '-d', default=os.environ['HOME'] + '/downloads/')
Then use args.direct
as the path to pass to glob.glob
.
Upvotes: 1