Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python索引路径_Python - Fatal编程技术网

如何使用python索引路径

如何使用python索引路径,python,Python,在Python中,如何使用计数器(其语法含义)索引路径?例如,我有以下代码: while i<5: with open("xxx\\xxx\\test(i)","r+") as f: f.write("hello") 而我使用.format()将索引添加到文件名中。还要记住包括i的初始化和增量,并使用写访问权限打开文件 i = 0 while i<5: with open("xxx\\xxx\\test({})".format(i), "w+") a

在Python中,如何使用计数器(其语法含义)索引路径?例如,我有以下代码:

while i<5:
    with open("xxx\\xxx\\test(i)","r+") as f:
        f.write("hello")
而我使用
.format()
将索引添加到文件名中。还要记住包括
i
的初始化和增量,并使用写访问权限打开文件

i = 0
while i<5:
    with open("xxx\\xxx\\test({})".format(i), "w+") as f:
        f.write("hello")
    i += 1
i=0

而我的回答是nx,我的回答是nx
from itertools import count
for i in count(0):
    if i >= 5:break
    with open(r"test {0}".format(str(i)), "w") as file:
        file.write("Hello World")