我如何用argparse打开文件夹
我想打开一个包含X Zipfiles的文件夹。
我有此代码:
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)
但是我收到错误代码: 许可证:[ERRNO 13]拒绝了权限:“测试”
我希望在运行代码时在数组中列出的文件夹中的zipfiles。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定为什么要遇到权限错误,但是在目录上使用
打开
不会做您想要的。 documentation python的open> open
要求,要求这样它的输入是 file 的字符串,而不是目录。如果将目录中的文件命名为字符串,请参阅我如何迭代给定目录中的文件?
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'sopen
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?
您确定您有权阅读这些文件吗?测试,无论它是什么文件或文件夹,似乎都无法从脚本中读取。
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.