How to open file using argparse?
Question:
I want to open file for reading using argparse
. In cmd it must look like: my_program.py /filepath
That’s my try:
parser = argparse.ArgumentParser()
parser.add_argument('file', type = file)
args = parser.parse_args()
This gives me [edit: added later from comment by OP]:
parser.add_argument('file', type = file) NameError: name 'file' is not defined
Answers:
The type of the argument should be string (which is default anyway). So make it like this:
parser = argparse.ArgumentParser()
parser.add_argument('filename')
args = parser.parse_args()
with open(args.filename) as file:
# do stuff here
Take a look at the documentation: https://docs.python.org/3/library/argparse.html#type
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()
print(args.file.readlines())
This implementation allows the “file name” parameter to be optional, as well as giving a short description if and when the user enters the -h
or --help
argument.
parser = argparse.ArgumentParser(description='Foo is a program that does things')
parser.add_argument('filename', nargs='?')
args = parser.parse_args()
if args.filename is not None:
print('The file name is {}'.format(args.filename))
else:
print('Oh well ; No args, no problems')
In order to have the file closed gracefully, you can combine argparse.FileType with the “with” statement
# ....
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()
with args.file as file:
print file.read()
— update —
Oh, @Wernight already said that in comments
I’ll just add the option to use pathlib
:
import argparse, pathlib
parser = argparse.ArgumentParser()
parser.add_argument('file', type=pathlib.Path)
args = parser.parse_args()
with args.file.open('r') as file:
print(file.read())
TL; DR
parser.add_argument(
'-f', '--file',
help='JSON input file',
type=argparse.FileType('r'),
)
Description
Simple command line script to reformat JSON files
reformat-json
-f package.json
--indent=2
--sort-keys
--output=sorted_package.json
can be code in Python as follows
#!/usr/bin/env python3
import argparse, json, sys
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-f', '--file',
help='JSON input file',
type=argparse.FileType('r'),
)
parser.add_argument(
'-i', '--indent',
help='Non-negative integer indent level',
type=int
)
parser.add_argument(
'-o', '--output',
help='Write JSON into output file',
type=argparse.FileType('w'),
)
parser.add_argument(
'-s', '--sort-keys',
action='store_true',
help='Sort output JSON by keys',
)
args = parser.parse_args()
if not args.file:
parser.print_usage()
return sys.exit(EXIT_FAILURE)
gson = json.dumps(
json.load(args.file),
indent=args.indent,
sort_keys=args.sort_keys
)
args.file.close()
if args.output:
args.output.write(gson)
args.output.write('n')
args.output.close()
else:
print(gson)
return sys.exit(EXIT_SUCCESS)
if __name__ == '__main__':
main()
I want to open file for reading using argparse
. In cmd it must look like: my_program.py /filepath
That’s my try:
parser = argparse.ArgumentParser()
parser.add_argument('file', type = file)
args = parser.parse_args()
This gives me [edit: added later from comment by OP]:
parser.add_argument('file', type = file) NameError: name 'file' is not defined
The type of the argument should be string (which is default anyway). So make it like this:
parser = argparse.ArgumentParser()
parser.add_argument('filename')
args = parser.parse_args()
with open(args.filename) as file:
# do stuff here
Take a look at the documentation: https://docs.python.org/3/library/argparse.html#type
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()
print(args.file.readlines())
This implementation allows the “file name” parameter to be optional, as well as giving a short description if and when the user enters the -h
or --help
argument.
parser = argparse.ArgumentParser(description='Foo is a program that does things')
parser.add_argument('filename', nargs='?')
args = parser.parse_args()
if args.filename is not None:
print('The file name is {}'.format(args.filename))
else:
print('Oh well ; No args, no problems')
In order to have the file closed gracefully, you can combine argparse.FileType with the “with” statement
# ....
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()
with args.file as file:
print file.read()
— update —
Oh, @Wernight already said that in comments
I’ll just add the option to use pathlib
:
import argparse, pathlib
parser = argparse.ArgumentParser()
parser.add_argument('file', type=pathlib.Path)
args = parser.parse_args()
with args.file.open('r') as file:
print(file.read())
TL; DR
parser.add_argument(
'-f', '--file',
help='JSON input file',
type=argparse.FileType('r'),
)
Description
Simple command line script to reformat JSON files
reformat-json
-f package.json
--indent=2
--sort-keys
--output=sorted_package.json
can be code in Python as follows
#!/usr/bin/env python3
import argparse, json, sys
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-f', '--file',
help='JSON input file',
type=argparse.FileType('r'),
)
parser.add_argument(
'-i', '--indent',
help='Non-negative integer indent level',
type=int
)
parser.add_argument(
'-o', '--output',
help='Write JSON into output file',
type=argparse.FileType('w'),
)
parser.add_argument(
'-s', '--sort-keys',
action='store_true',
help='Sort output JSON by keys',
)
args = parser.parse_args()
if not args.file:
parser.print_usage()
return sys.exit(EXIT_FAILURE)
gson = json.dumps(
json.load(args.file),
indent=args.indent,
sort_keys=args.sort_keys
)
args.file.close()
if args.output:
args.output.write(gson)
args.output.write('n')
args.output.close()
else:
print(gson)
return sys.exit(EXIT_SUCCESS)
if __name__ == '__main__':
main()