Skip to content
Advertisement

How can I open a folder with argparse

I want to open a folder containing x zipfiles.

I have this code:

parser = argparse.ArgumentParser(description='Test')
parser.add_argument("foldername", help="Foldername of log files archive to parse")
args = parser.parse_args()
with open(args.foldername) as files:
    filename = args.filename
    print(filename)

But I get error code: PermissionError: [Errno 13] Permission denied: ‘Test’

I would like the zipfiles in the folder to be listed in an array when I run the code.

Advertisement

Answer

I’m not sure why you’re getting a permission error, but using open on a directory won’t do what you want. The documentation for Python’s open requires that its input is a string to a file, not a directory.

If you want the files in a directory given the name of the directory as a string, refer to How can I iterate over files in a given directory?

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement