0

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.

  • You don't have the permission to read the file. Further, if it is a folder, you have to use `os.listdir(args.foldername)` to get all files, then iterate through the list for reading by `os.path.join(args.foldername, filename)`. – Elkan Jun 28 '22 at 16:20

2 Answers2

1

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?

zylitoL
  • 26
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 28 '22 at 09:26
  • Thank you, I managed to get an array with the zip files inside the folder. However, I now get a new error. I run a for loop. for files in range(len(filename)): archive = zipfile.ZipFile(filename[files], 'r') I get new error: FileNotFoundError: [Errno 2] No such file or directory: 'test.zip'. Do you have any suggestions? – Olovia.solfjell Jun 28 '22 at 12:48
0

Are you sure you have permission to read those files? Test, whatever file or folder it is, doesn't seem to be readable from your script.

Vancha
  • 63
  • 1
  • 8