python - 解析目录作为参数的问题
问题描述
我在 python 中有一个程序,它遍历目录中的所有文件,我将其作为参数给出。我还在我的 python 文件中设置了一个参数解析函数。当我尝试使用 bash 脚本和目录作为参数运行该 python 文件时,出现以下错误:
Traceback (most recent call last):
File "main.py", line 93, in <module>
args['inverse transformation path'], args['line'])
File "main.py", line 68, in main
transform_path, inverse_path, line)])
File "main.py", line 65, in main
for filename in os.listdir(image_directory):
NotADirectoryError: [Errno 20] Not a directory: 'test1/2020-03-11-11-45-37-576.jpg'
我正在运行 python 脚本:
python3 main.py -imd 'test1' -pp points.csv -rpp transformed_points.csv -tp transformation_matrix.csv
-itp inverse_transformation_matrix.csv -l 2
这就是我正在运行的功能。
def main(image_directory, points_path, real_points_path, transform_path, inverse_path, line):
data = []
index = 0
for filename in os.listdir(image_directory):
if filename.endswith(".jpg") or filename.endswith(".png"):
data.append([filename, analysis_main(os.path.join(image_directory, filename), points_path, real_points_path,
transform_path, inverse_path, line)])
index += 1
df = pandas.DataFrame(data)
df.to_csv("razdalje.csv", sep=',', header=False, index=False)
和解析
ap = argparse.ArgumentParser()
ap.add_argument("-imd", "--image directory", required=True,
help="directory with images")
ap.add_argument("-pp", "--points path", required=True,
help="path to the csv file with pixels used for declaring coordinate system")
ap.add_argument("-rpp", "--real points path", required=True,
help="path to the csv file with points on the ground used for declaring coordinate system")
ap.add_argument("-tp", "--transformation path", required=True,
help="path to the csv file with matrix used for transforming space")
ap.add_argument("-itp", "--inverse transformation path", required=True,
help="path to the csv file with inverse matrix used for transforming space")
ap.add_argument("-l", "--line", required=True,
help="declaring on which lane is the analysis being done, 1 -> left lane 2 -> right lane")
args = vars(ap.parse_args())
并运行代码
main(args['image directory'], args['points path'], args['real points path'], args['transformation path'],
args['inverse transformation path'], args['line'])
解决方案
我们不知道您是如何浏览文件系统的,但列出的文件显然不是目录,而是图像。在检查之前添加这样的安全检查:
if os.path.isdir(image_directory):
for filename in os.listdir(image_directory):
我建议你调查一下os.walk()
,因为它对你有用。
推荐阅读
- python - 如何使用参数扩展 docker 容器
- java - 序列化过程不断地覆盖自己?
- javascript - discord.js - 命令正在添加更多要嵌入的字段
- javascript - (节点:9374)警告:要加载 ES 模块,请设置“类型”:“模块”
- python - 如何遍历多个列表?
- rust - 我的空 rust 项目无法编译,我该怎么办?
- python - 如何在文本框中查找和输入文本?
- flutter - 删除路由会释放 Provider 并引发错误
- ruby-on-rails - 在rails中将数组转换为哈希
- hyperledger-fabric - 我如何部署在超级账本结构中创建合约的以太坊智能合约