我在这里遇到代码问题。我似乎无法使argparse参数起作用。理想情况下,代码应该运行,并且我应该能够在命令提示符下解析参数。如果运行脚本,它将打印命名空间(c = False,d = False,f = False,path ='。',s = False)我如何编写它以便代码正确执行?
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-c', help='counts the number of files in folder', action='store_true')
parser.add_argument('-d', help='returns a dictionary as filename: {}, filesize: {}', action='store_true')
parser.add_argument('-f', help='returns a dictionary which contains foldername, size of the folder and subfiles', action='store_true')
parser.add_argument('-s', help='gives the total number of bytes in folder and subfolders', action='store_true')
parser.add_argument('-path', metavar='<path to folder>', default='.', help='path for counting')
args = parser.parse_args()
print(args)
counter = 0
size = 0
dict = {'.': 0}
for parent, dirnames, files in os.walk(args.path):
for filenames in files:
if args.s:
size += s
print("Total bytes: ", size)
if args.d:
print(filename, s, 'b')
if args.f:
if parent in dict:
dict[parent] += s
else:
dict[parent] = s
if args.c:
count += 1
print("Number of files: ", counter)
if args.f:
for files in dict:
print(files, dict.get(files), 'b')
我希望我知道你的问题是什么。如果我理解正确,请投票(肯定)(问题):)。因此,转到文件(上,左),然后转到设置。按设置。然后按下按钮左侧的箭头,上面写有“项目:_______”(_______ =项目名称)。然后按Project Interpreter。在这里,您将看到已经在项目中安装的所有软件包(例如agrparse,pygame等)。如果您不想使用新键盘(在这种情况下为:argparse),请按+(该窗口的右侧),然后进行搜索。找到它之后,单击它,然后在左侧的下方,找到一个显示安装软件包的按钮。按下它,等待其安装。然后,您完成了。您将一次又一次地在所有新项目中执行此操作。