我需要对目录中的文件列表进行操作。使用 while 循环循环遍历所有文件并需要附加到文件名“i”。
我将代码放入 while 循环中并尝试追加,但它不起作用..
while x in range(101):
print(x)
file = x.file
print(file)
# code block to execute on each file
else:
print("all files processed")
您可以使用字符串格式化运算符将索引附加到文件名中:
当 x 在范围(101)内时: 打印(x) 文件 = '{}.file'.format(x) 打印(文件) # 在每个文件上执行的代码块 别的: print("所有文件已处理")