【问题标题】:creating multiple file using os python使用 os python 创建多个文件
【发布时间】:2022-11-25 00:44:19
【问题描述】:
大家好,我正在使用 ros noetic,我必须创建 12 个文件名作为 x.bag,x 范围最大为 12。代码如下。
import rospy
import os
for x in range(12):
cmd='rosbag record -o /home/mubashir/catkin_ws/src/germany1_trush/rosbag/x.bag /web_cam --duration 5 '
os.system(cmd)
我如何在 cmd 中获取 x 的值。
使用 os 创建 12 个持续时间为 5 秒的文件。虽然名称不同,但我无法在 cmd 中访问 x 的值
【问题讨论】:
标签:
python
linux
for-loop
【解决方案1】:
我不确定我是否完全理解你的问题。我想你想要的是运行以下命令 12 次(从 0 到 11):
import rospy
import os
for x in range(12):
cmd = f'rosbag record -o /home/mubashir/catkin_ws/src/germany1_trush/rosbag/{x}.bag /web_cam --duration 5'
os.system(cmd)
您可能想要 1..12,您可以使用 {x + 1} 轻松完成。
顺便说一句,这叫做“Literal String Interpolation”,又名 f-string。非常方便。